Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player rotation affects other player rotation

Discussion in 'Multiplayer' started by Emmsii, Oct 28, 2015.

  1. Emmsii

    Emmsii

    Joined:
    Feb 27, 2014
    Posts:
    9
    I've been experimenting with UNET after following a few tutorials and I'm running into a problem.

    I have to clients connecting to each other successfully, there position is synced fine as is rotation. But when one character rotates, the other slightly rotates. Here's what happens, I'm rotating the player on the left, the player on the right is rotating whenever the other rotates.



    The player object has has a movement script and a camera as a child.
    Here's my PlayerMovement script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerMovement : NetworkBehaviour {
    6.  
    7.     public float speed = 6f;
    8.     public Camera playerCamera;
    9.     public float cameraSmoothing = 10f;
    10.  
    11.     private Rigidbody rb;
    12.     private Vector3 movement;
    13.     private float hValue;
    14.     private float vValue;
    15.     private int floorMask;
    16.  
    17.     private float camRayLength = 100f;
    18.     private Vector3 cameraOffset;
    19.  
    20.     void Awake () {
    21.         rb = GetComponent<Rigidbody> ();
    22.         floorMask = LayerMask.GetMask ("Floor");
    23.         playerCamera.transform.parent = null;
    24.         cameraOffset = playerCamera.transform.position - transform.position;
    25.     }
    26.  
    27.     void Update () {
    28.         hValue = Input.GetAxisRaw ("Horizontal");
    29.         vValue = Input.GetAxisRaw ("Vertical");
    30.     }
    31.  
    32.     void FixedUpdate(){
    33.         CameraMove ();
    34.         Move ();
    35.         Turn ();
    36.     }
    37.  
    38.     private void Move(){
    39.         movement.Set (hValue, 0f, vValue);
    40.         movement = movement.normalized * speed * Time.deltaTime;
    41.         rb.MovePosition (transform.position + movement);
    42.     }
    43.  
    44.     private void Turn(){
    45.         Ray camRay = playerCamera.ScreenPointToRay (Input.mousePosition);
    46.         RaycastHit floorHit;
    47.      
    48.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    49.             Vector3 playerToMouse = floorHit.point - transform.position;
    50.             playerToMouse.y = 0f;
    51.          
    52.             Quaternion newRot = Quaternion.LookRotation(playerToMouse);
    53.             rb.MoveRotation(newRot);
    54.         }
    55.     }
    56.  
    57.     private void CameraMove(){
    58.         Vector3 targetCamPos = transform.position + cameraOffset;
    59.         playerCamera.transform.position = Vector3.Lerp (playerCamera.transform.position, targetCamPos, cameraSmoothing * Time.deltaTime);
    60.     }
    61. }
    62.  
    The player also has a PlayerSetup script to disable behaviors if the player is not local.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerSetup : NetworkBehaviour {
    6.  
    7.     [SerializeField]
    8.     private Behaviour[] toDisable;
    9.  
    10.     private Camera sceneCamera;
    11.  
    12.     private void Start(){
    13.         if (!isLocalPlayer) {
    14.             for (int i = 0; i < toDisable.Length; i++) {
    15.                 if(!toDisable[i])
    16.                     continue;
    17.                 toDisable [i].enabled = false;
    18.             }
    19.         } else {
    20.             sceneCamera = Camera.main;
    21.             if(sceneCamera){
    22.                 sceneCamera.gameObject.SetActive(false);
    23.             }
    24.         }
    25.     }
    26.  
    27.     private void OnDisable(){
    28.         if (sceneCamera) {
    29.             sceneCamera.gameObject.SetActive (true);
    30.         }
    31.     }
    32. }
    33.  
    I have no idea what's going on, I don't see any way how one player can affect the other's rotation.
     
    Last edited: Oct 28, 2015
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    try putting isLocalPlayer check in the update functions:

    1. void Update () {
    2. if (!isLocalPlayer) return;

    3. hValue = Input.GetAxisRaw ("Horizontal");
    4. vValue = Input.GetAxisRaw ("Vertical");
    5. }

    6. void FixedUpdate(){
    7. if (!isLocalPlayer) return;

    8. CameraMove ();
    9. Move ();
    10. Turn ();
    11. }
     
  3. Emmsii

    Emmsii

    Joined:
    Feb 27, 2014
    Posts:
    9
    Just gave that a go, still get the exact same thing. I don't know if its something to do with the Raycasting or the player cameras.

    The prefab looks like this:


    When the players are spawned in their camera is removed as a child objects, because of line 23 in the PlayerMovement script.

    If I don't remove the camera transform parent, very funky things happen:
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    can you post the project that reproduces this?
     
  5. Emmsii

    Emmsii

    Joined:
    Feb 27, 2014
    Posts:
    9
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    it is because your raycast in Turn() is running when the application doesn't have focus - on both clients at the same time.
     
  7. Emmsii

    Emmsii

    Joined:
    Feb 27, 2014
    Posts:
    9
    Ahh! Well the issue has been fixed.

    In the PlayerMovement script, I added the following function and variable:

    Code (CSharp):
    1. private bool focus = true;
    2.  
    3.     void OnApplicationFocus(bool focusStatus){
    4.         focus = focusStatus;
    5.     }
    Then changed my FixedUpdate function to this:

    Code (CSharp):
    1.     void FixedUpdate(){
    2.         if (!focus)
    3.             return;
    4.  
    5.         Move ();
    6.         Turn ();
    7.         CameraMove ();
    8.     }
    I don't need to include if(!isLocalPlayer) return; in my Update & FixedUpdate functions, the PlayerMovement script is automatically disabled if its not the local player.

    Thanks for your help!