Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera moving to other players in network Third Person

Discussion in 'Scripting' started by byro210, Nov 17, 2017.

  1. byro210

    byro210

    Joined:
    Feb 18, 2014
    Posts:
    121
    The camera works fine in singleplayer but when i try it online it jumps to the last logged on Player, i want it to stop but if i put if (isLocalPlayer) it completely stops the third person camera from moving around here is the script im not sure what to do to fix it.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class ThirdPersonCamera : NetworkBehaviour
    6. {
    7.  
    8.     public Transform target;
    9.     public float distance = 5.0f;
    10.  
    11.     public float bufferup = 1.5f;
    12.     public float bufferright = 0.75f;
    13.  
    14.     public float xSpeed = 250.0f;
    15.     public float ySpeed = 120.0f;
    16.  
    17.     public float yMinLimit = -20f;
    18.     public float yMaxLimit = 80f;
    19.  
    20.     private float x = 0.0f;
    21.     private float y = 0.0f;
    22.  
    23.     // Use this for initialization
    24.     void Update()
    25.     {
    26.         if (isLocalPlayer)
    27.  
    28.         {
    29.             Cursor.lockState = CursorLockMode.Locked;
    30.             Vector3 angles = transform.eulerAngles;
    31.             x = angles.y;
    32.             y = angles.x;
    33.  
    34.             // Make the rigid body not change rotation
    35.             if (GetComponent<Rigidbody>())
    36.                 GetComponent<Rigidbody>().freezeRotation = true;
    37.         }
    38.  
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void LateUpdate()
    43.    
    44.  
    45.     {
    46.  
    47. //If i add if(isLocalPlayer) here it stops the camera rotating when i move the mouse :(
    48.  
    49.       if (target)
    50.        
    51.  
    52.  
    53.             {
    54.                // distance -= .5f * Input.mouseScrollDelta.y;
    55.                // if (distance < 0)
    56.                // {
    57.                   //  distance = 0;
    58.                 //}
    59.  
    60.                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    61.                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    62.  
    63.                 y = ClampAngle(y, yMinLimit, yMaxLimit);
    64.  
    65.                 Quaternion rotation = Quaternion.Euler(y, x, 0);
    66.                 Vector3 position = rotation * new Vector3(bufferright, 0.0f, -distance) + target.position + new Vector3(0.0f, bufferup, 0.0f);
    67.  
    68.                 transform.rotation = rotation;
    69.                 transform.position = position;
    70.             }
    71.         }
    72.  
    73.  
    74.  
    75.  
    76.     float ClampAngle(float angle, float min, float max)
    77.         {
    78.  
    79.         if (angle < -360)
    80.             angle += 360;
    81.         if (angle > 360)
    82.             angle -= 360;
    83.         return Mathf.Clamp(angle, min, max);
    84.     }
    85.  
    86. }
    Please does anyone know how i could change this to fix it and Thankyou :)
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Generally you should not be making your camera control script a network script like this, or put a networkidentity on your camera at all (unless it is a camera that all players should be watching and will be moved around by the server), otherwise you're basically sharing the same camera for all players. Make the script a monobehavior, take out all the networking stuff like isLocalPlayer (isLocalPlayer is something you only put on player objects anyway), and remove the NetworkIdentity from the camera.

    On a script on your player object, have it check for isLocalPlayer, and if true have it find the camera object, and set itself as the target. See if that works for you.

    Do something like this on the player gameobject:
    Code (csharp):
    1.  
    2.  
    3. GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraControlScript>().target = gameObject.transform;
    4.  
    5.  
    Sorry I don't have Unity in front of me to check if I got that syntax exactly right, but if not it is close.
     
  3. byro210

    byro210

    Joined:
    Feb 18, 2014
    Posts:
    121
    I add it to a new script but it just says
    Object Reference not set to an instance of an object FindTarget.Start() (at Assets/FindTarget.cs:18)

    ive been playing with it for a few hours now and i havent been able to fix that