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

JS Mouselook script only works on one axis at a time?

Discussion in 'Scripting' started by senox13, Jul 1, 2014.

  1. senox13

    senox13

    Joined:
    Oct 10, 2013
    Posts:
    4
    I want to start by saying that I have next to no experience with Java, JavaScript, or Unity. I do, however, understand the basics, more or less, and was able to make a script for character movement with sprinting and stamina. What I've been working on next is a mouselook script. The problem I have is that only one axis will work at a time.

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. public var Sensitivity : int = 3;
    5. private var RotationY : short = 0.0;
    6.  
    7. function Update(){
    8.     RotationY += Input.GetAxis("Mouse Y") * Sensitivity;
    9.     transform.localRotation = Quaternion.AngleAxis (RotationY, Vector3.left);
    10. }
    11.  
    This makes the up and down movement work fine.

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. public var Sensitivity : int = 3;
    5. private var RotationY : short = 0.0;
    6. private var RotationX : short = 0.0;
    7.  
    8. function Update(){
    9.     RotationY += Input.GetAxis("Mouse Y") * Sensitivity;
    10.     transform.localRotation = Quaternion.AngleAxis (RotationY, Vector3.left);
    11.  
    12.     RotationX += Input.GetAxis("Mouse X") * Sensitivity;
    13.     transform.localRotation = Quaternion.AngleAxis (RotationX, Vector3.up);
    14. }
    15.  
    This only makes the side-to-side axis rotate.

    I'm sure i'm missing something stupidly obvious to anyone who knows what they're doing. Please help.
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You have overwritten the localRotation each time. You need to combine both rotations before assigning to localRotation.
     
  3. senox13

    senox13

    Joined:
    Oct 10, 2013
    Posts:
    4
    How exactly do I do that?