Search Unity

Mouselook script multiplayer

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

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys im trying to make a multiplayer mouselook script but the only problem i have right now is that my capsule also gets rotated but i only want my camera to rotate like it should, my script is attached to my capsule because otherwise it will move all characters...
    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.     public Camera cam;
    16.  
    17.     private void Start()
    18.     {
    19.  
    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.         cam.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, cam.transform.up);
    39.     }
    40. }
    41.  
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I don't understand, if you don't want your capsule to rotate, comment out line 37.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Yeah but i wrote -Mouselook y but it still makes me able to transform on y axis
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you don't want to rotate the capsule, and you have this script attached to the capsule, don't modify transform.localRotation on line 37. If you want to rotate the camera then you would do all your rotations to cam.transform.localRotation like you're doing on line 38.
     
    Vexer likes this.
  5. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    I want to make my capsule only to rotate on the x axis..
     
  6. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Why would making it negative stop it from happening? Just remove the entire line. You're setting your localRotation, so of course your capsule is rotating.
     
  7. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Ohhhh...

    Try using RotateAround instead. RotateAround lets you specify which (local) axis you rotate around.
     
  8. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    That made my character space around and rotate every axis could you please edit my code to where it should work??
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That sounds like you're still messing with the object's own transform instead of the camera's transform. If your code starts with "transform" instead of "cam.transform" you're having your script manipulate the capsule instead of the camera.

    See your own previously posted code. On line 37 you are modifying "transform.rotation", transform refers to the object the script is attached to, which is the capsule. On line 38 you are modifying "cam.transform.rotation", which instead of the capsule's transform you are modifying the transform of the camera. So whatever you are doing, you want to manipulate "cam.transform" not "transform". get it?
     
  10. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    I get that but i want my capsule to transform on it's x axis but not on it's Y axis that's why i wrote -Mouselook.y but it still uses it's y axis..