Search Unity

all characters looking around problem..

Discussion in 'Multiplayer' started by Vexer, May 21, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys i made this mouse look script for a single player game long ago and now im trying to convert it into multiplayer could someone help me i've removed all my mutliplayer code because it wasn't working please help me! :) My problem right now is that if i look around with 1 character/game it let's all my games look around

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class camMouseLook : MonoBehaviour
    7. {
    8.  
    9.     Vector2 mouseLook;
    10.     Vector2 smoothV;
    11.     public float sensitivity = 5.0f;
    12.     public float smoothing = 2.0f;
    13.  
    14.     GameObject character;
    15.  
    16.     private void Start()
    17.     {
    18.         character = this.transform.parent.gameObject;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxis("Mouse Y"));
    24.  
    25.         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    26.         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
    27.         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
    28.         mouseLook += smoothV;
    29.         mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
    30.  
    31.         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
    32.         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    33.     }
    34. }
    35.  
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    Just put something in Update() that checks to see if this client owns the object. If it doesn't, just return;.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    I tried this: if (!isLocalPlayer) return; but it didn't work..
     
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Show us that code. As is right now, you inherit from MonoBehavior which does not have a isLocalPlayer field. My guess is that you didn't spawn your object on the network.
     
  5. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    This is it:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6.  
    7. public class camMouseLook : NetworkBehaviour
    8. {
    9.  
    10.     Vector2 mouseLook;
    11.     Vector2 smoothV;
    12.     public float sensitivity = 5.0f;
    13.     public float smoothing = 2.0f;
    14.  
    15.     GameObject character;
    16.  
    17.     private void Start()
    18.     {
    19.         character = this.transform.parent.gameObject;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (!isLocalPlayer)
    25.         {
    26.             return;
    27.         }
    28.  
    29.         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxis("Mouse Y"));
    30.  
    31.         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    32.         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
    33.         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
    34.         mouseLook += smoothV;
    35.         mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
    36.  
    37.         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
    38.         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    39.     }
    40. }
    41.  
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    First off, you can only have a single NetworkIdentity component per gameobject with no NetworkIdentity components on child objects. The NetworkIdentity component has to be on the root gameobject. NetworkBehaviour components have to be on the same gameobject as the NetworkIdentity component, so also need to be on the root gameobject.

    The way you are grabbing the reference to your character object in Start says that you're putting this on a child object, which is not supported.