Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Tracking where the players are looking in MLAPI?

Discussion in 'Multiplayer' started by bunnyscout238, Sep 13, 2021 at 9:59 PM.

  1. bunnyscout238


    Nov 6, 2018
    I've been working on a very simple fps using MLAPI and I have gotten pretty much everything working. I have my players set up so that the camera is a child of the player and the gun is a child of the camera. Then I use a script to move around the camera with the gun following it. Naturally unity is keeping track of which direction on the x axis the player is turned towards, because I have a NetworkTransform attached to the player. However, unity is not keeping track of where the player is looking on the y axis (up or down). You just see all the other players as looking straight ahead.

    This is my code for the camera:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (playerBody.GetComponent<HelloWorldPlayer>().isPlayer)
    4.         {
    5.             float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    6.             float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    8.             xRotation -= mouseY;
    9.             xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    11.             transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    13.             playerBody.transform.Rotate(Vector3.up * mouseX);
    14.         }
    15. }
    And this is a small amount of code for disabling components:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         isPlayer = true;
    4.         if (!IsLocalPlayer)
    5.         {
    6.             isPlayer = false;
    7.             GetComponent<Move>().enabled = false;
    8.             camer.GetComponent<Camera>().enabled = false;
    9.             camer.GetComponent<AudioListener>().enabled = false;
    10.         }
    11. }
    I've tried a bunch of different things but none have seemed to work, especially since I don't fully understand ServerRpcs and other mlapi things.

    Is there a simple solution that I'm missing?