#Positions not updating properly

1 messages · Page 1 of 1 (latest)

lavish crest
#

Honestly there's a lot going wrong here and I'm unsure how to explain them well so:
(All of these issues stem from the client joining the host)

  • The host cannot move and is stuck in the spawn position in both the POV of the host and the client, the host loses the ability to turn his camera. Also the client's position isn't updating for the host.
  • The client can move although the movement is different (as in, moving left and right is slower whilst moving forwards and back is faster.

The movement is fine when there's only a host or when I just send in a singular character although everything breaks when I try to make it so two players are connected. I'll send the code I'm using for player movement in a sec along with the network manager script.
(This is my first time trying to implement multiplayer into any of my games)

#

Network manager script

using UnityEngine.UI;
using Unity.Netcode;

public class NetworkManagerUI : MonoBehaviour
{
    [SerializeField] private Button serverBtn;
    [SerializeField] private Button hostBtn;
    [SerializeField] private Button clientBtn;

    private void Awake()
    {
        serverBtn.onClick.AddListener(() =>
        {
            NetworkManager.Singleton.StartServer();
        });
        hostBtn.onClick.AddListener(() =>
        {
            NetworkManager.Singleton.StartHost();
        });
        clientBtn.onClick.AddListener(() =>
        {
            NetworkManager.Singleton.StartClient();
        });
    }   
}
#

Cam controller script

using Unity.Netcode;
using UnityEngine;

public class CamController : NetworkBehaviour
{

    public Transform FollowTarget, LookTarget;
    public float FollowSpeed = 10f;


    private void LateUpdate() //used bc. updates after all movements have happened in update frame
    {
        if (!IsOwner)
            return; // If not the owner of the script, don't run code

        Vector3 targetPosition = FollowTarget.position; //
        transform.position = Vector3.Lerp(transform.position, targetPosition, FollowSpeed * Time.deltaTime); //lerp will move from curr, to targetpos smoothly

        transform.LookAt(LookTarget); //rotate cam to look at specified target

    }

}
#
using UnityEngine.InputSystem;
using Unity.Netcode;

public class InputHandler : NetworkBehaviour
{

    public PlrController CharController;

    private InputAction _moveAction, _lookAction, _jumpAction;

   
    void Start()
    {
        _moveAction = InputSystem.actions.FindAction("Move"); //These actions in theory, tie into the Input System hooks (so on keyboard, wasd, works for move)
        _lookAction = InputSystem.actions.FindAction("Look");
        _jumpAction = InputSystem.actions.FindAction("Jump");

        _jumpAction.performed += OnJumpPerformed;

        Cursor.visible = false; // get rid of dot in middle (replaced with a reticle or smthn perhaps)

    }


    void Update()
    {
        if (!IsOwner)
            return; // If not the owner of the script, don't run code

        Vector2 movementVector = _moveAction.ReadValue<Vector2>(); //return the (x,y) vector stored in the move action (ex. w held (0,1) w,d (1,1) or -1, 1 etc.) - amt to move
        CharController.Move(movementVector); //we will have a function 'Move' in our PlrController script, that uses this force to push player that direction

        Vector2 lookVector = _lookAction.ReadValue<Vector2>(); // camera is also (x,y force) bc you can only rotate up/down and left/right, tho it applies on a 3d scale
        CharController.Rotate(lookVector);
    }

    private void OnJumpPerformed(InputAction.CallbackContext context)
    {
        CharController.Jump();
    }


}
#

I've been stuck on this for a day or two now and I'm kinda running out of ideas

#

I can post more if anyone needs to see anything to help

#

Thanks

wet radish
#

You'll need to disable the camera and character controller for every player object where IsLocalPlayer is false.

lavish crest
#

added

   CharController = null;```
and 
```if (!IsLocalPlayer)
   curCam.enabled = false;```
which did fix the host being frozen although they're still unsynced
#

(curCam is the camera)

wet radish
lavish crest
#

Changing the script to CharController.enabled = false; is just leading to the same issue as shown in the video

#

Could it be something wrong with the settings of the Network Transform

wet radish
#

You mean its not dropping to the ground? Check in the editor that the collider is still correct on the floating player.

#

if it has a rigidbody then you'll need to use a network rigidbody as well

lavish crest
#

It doesn't have a rigidbody

lavish crest