Moving Your Player Around
Get your player objects moving around and synchronized!
Client-authoritative movement script
2
Editing the script
using FishNet.Object;
using UnityEngine;
// Inherit from NetworkBehaviour instead of MonoBehaviour
public class PlayerMovement : NetworkBehaviour
{
public float MoveSpeed = 5f;
private void Update()
{
// Only run this code on the object the local client owns.
// This prevents us from moving other players' objects.
if (!IsOwner)
return;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(horizontal, 0f, vertical);
if (moveDirection.magnitude > 1f)
moveDirection.Normalize();
transform.position += MoveSpeed * Time.deltaTime * moveDirection;
}
}
Last updated


