Search Unity

How to get MouseLook to stay strictly in either X or Y planes

Discussion in 'Scripting' started by Zaffer, Aug 13, 2014.

  1. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi, it's been a while since I posted here. Can't figure out how to indicate code or link, so it's all in the same font. Sorry.

    I am trying to modify the MouseLook script to force the mouse motion to stay strictly in the X or Y planes rather than be able to tilt. Here's my code:

    if (Input.GetAxis("Mouse X")){
    transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    }
    if (Input.GetAxis ("Mouse Y")){
    transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, 0, 0);
    }

    Of course this doesn't work because Input.GetAxis returns a float and I need a boolean value. Is there any way to address Input.GetAxis to find out whether it is using Mouse X or Mouse Y at any given moment. Thanks.

    Zaffer

    Cross posted this to Unity Answers at http://answers.unity3d.com/questions/769415/how-to-get-mouselook-to-stay-strictly-in-either-x.html -- no answer
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Setting the Axes property in the inspector to only one axis is not what you want?
     
  3. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Unity-post.jpg Thanks Fraconte,

    I'm making a color cube (3D depiction of the RGB color space) that you can go into and move around in like a room. This works well if the MouseLook axis is set to either MouseX or MouseY, but if it is set to MouseXAndY, the view tilts which is not what I want. I'm trying to figure out a way to have the view move strictly as either in MouseX or MouseY but not tilt (see attached image). That's why I'm trying to figure out how to access Input.GetAxis and have it tell me whether the mouse is moving in X or Y at any given moment so I can constrain the motion to only X or Y axes. If you or anyone has any idea on how to do this, I'd very much appreciate it. Thanks.
     
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    EDIT: I totally missed your second post. I see the problem now.

    Code (CSharp):
    1. float rotX;
    2. float rotY;
    3.  
    4. void Update()
    5. {
    6.     rotX += Input.GetAxis("Mouse X");
    7.     rotY = Mathf.Clamp(rotY - Input.GetAxis("Mouse Y"), -85.0f, 85.0f);
    8.  
    9.     transform.localEulerAngles = new Vector3(rotY, rotX, 0.0f);
    10. }
    I didn't test this, so let me know if it works.
     
    Last edited: Aug 13, 2014
  5. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky,
    Thanks for your code. Unfortunately, I can't use "transform.localEulerAngles" because if I do it makes the MouseLook view change in unexpected ways that I don't want. See this post: http://answers.unity3d.com/questions/759262/character-controller-rotation-wont-accept-script-v.html.

    So I adapted your approach to using "transform.Rotate" which sort of works. It will keep the mouse traveling in either X or Y direction but it's still easy to get things tilted because if the mouse is tilted when it starts rotating, of course, it will stay tilted. If only I could make the character controller/camera just *stay upright* all the time! Here's my code. Any suggestions will be appreciated.

    By the way, how did you get your code into a code box. I can't find a button for that. Thanks.

    if(Input.GetAxis("Mouse X") > 0){

    rotY = Mathf.Clamp((rotY - Input.GetAxis("Mouse Y")), -85.0f, 85.0f);
    transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    Debug.Log ("Mouse X");
    }

    if(-Input.GetAxis("Mouse Y") < 0){

    rotX = Mathf.Clamp(-85.0f, (rotX - Input.GetAxis ("Mouse X")), 85.0f);
    transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, 0, 0);
    Debug.Log ("Mouse Y");
    }

    else if(Input.GetAxis ("Mouse X") == 0 && (-Input.GetAxis ("Mouse Y") == 0)){
    Debug.Log("no movement");
    }
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I tryed a box (of 6 planes) with one camera inside: if i attach the mouselook script to the box i get the tilt you say, but if i attach the script to the camera it works ok.

    Are you moving the box? And why not the camera?

    Edit: for the code box use the "Insert..." button (near the films icon button)
     
  7. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    If your problem with localEulerAngles is the same as the one in the link, simply use my old code and add this:

    Code (CSharp):
    1. void Awake()
    2. {
    3.     rotX = transform.localEulerAngles.y;
    4.     rotY = transform.localEulerAngles.x;
    5. }
    If this is not the only problem you have with localEulerAngles, what else? Just curious, as I use localEulerAngles for my camera and I'm interested in knowing if there's any problems with it I'm unaware of.

    Moving the box on the mouse X axis (object Y axis) is good because then it sets transform.forward to the box's forward vector and you can simply multiply transform.forward and transform.right by movement speed to get a movement vector.

    In my FPS, I use a weird, hacky solution. I have my camera GameObject as a child of the player GameObject. My MouseLook script adjusts the mouse X (object Y) rotation of the PARENT (which in turn also rotates the camera), while the mouse Y (object X) rotates the camera. This is so my player objects is always upright, but also enables looking up and down.
     
  8. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    With "box" I mean the room where the character controller is walking into. The camera setup you are talking about I think is the same of the standard 1st person character controller that works fine too in the room with no "tilting".
    So or Zaffer is moving the room or have a modified setup of the character controller that generates "tilt".
     
  9. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    HI Fraconte,

    I am using my own scripts for Character Controller and MouseLook, but I tried the default ones and got the same result, lots of tilting. I don't know how you got your results of no tilting.

    I posted a build of my cube at http://www.bitsong.com/forPosting/Unity/Color%20Cube_Build.html/Color%20Cube_Build.html
    For this build I used my original code with the tilt problem -- a very stripped down version of the MouseLook script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu("Camera-Control/Mouse Look")]
    5. public class MouseLook_bp1 : MonoBehaviour {
    6.  
    7.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    8.     public RotationAxes axes = RotationAxes.MouseXAndY;
    9.     public float sensitivityX = 10F;
    10.     public float sensitivityY = 10F;
    11.  
    12.        
    13.     void Update ()
    14.     {
    15.  
    16.         if (axes == RotationAxes.MouseXAndY)
    17.         {
    18.    
    19.             transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, Input.GetAxis("Mouse X") * sensitivityX, 0);
    20.         }
    21.  
    22.         else if (axes == RotationAxes.MouseX)
    23.         {
    24.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    25.  
    26.         }
    27.         else
    28.         {
    29.             transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, 0, 0);
    30.  
    31.         }
    32.     }
    33.  
    34. }
     
  10. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky,

    Thanks Cranky, I will try this and let you know if it works -- sound like it might.

    My other problem with localEulerAngles is that I am also planning to have a feature where the user can input two colors, like 204 153 153 and 0 0 0 by clicking on a palette of the 216 "Web Safe" colors which are the spheres in my model. The controller/camera will then snap to line up those two colors so the user can "surf" from one color to the other. You can see how this works by typing the "t" key in my running build at http://www.bitsong.com/forPosting/Unity/Color%20Cube_Build.html/Color%20Cube_Build.html

    Here is the code for my FPSWalker script with the snapping code included at the bottom. It's a very simplified controller script.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var speed = 8.0;
    4. private var moveDirection = Vector3.zero;
    5.  
    6. function FixedUpdate () {
    7.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    8.         moveDirection = transform.TransformDirection(moveDirection);
    9.         moveDirection *= speed;
    10.                    
    11.         if(Input.GetButton("Test")){
    12.             PlaceFPC();
    13.         }
    14.        
    15.     var controller : CharacterController = GetComponent(CharacterController);
    16.     var flags = controller.Move(moveDirection * Time.deltaTime);
    17. }
    18.  
    19. function PlaceFPC (){
    20.     var obj2 = GameObject.Find ("204 153 153");
    21.     var obj1 = GameObject.Find ("0 0 0");
    22.    
    23.     var dir : Vector3 = obj1.transform.position - obj2.transform.position;
    24.     Debug.Log ("dir: " + dir);
    25.    
    26.     var midPoint : Vector3 = obj2.transform.position + dir / 2.0;
    27.     var endPoint : Vector3 = obj2.transform.position;
    28.     this.transform.position = endPoint;
    29.    
    30.     var rot : Quaternion = Quaternion.LookRotation(dir);
    31.     this.transform.rotation = rot;
    32.     Debug.Log ("rot: " + rot);
    33.  
    34. }
    35. @script RequireComponent(CharacterController)
    This feature doesn't work at all if I use the localEulerAngles code. Only works with the transform.Rotate code. I'm sorry, I'm not much of a coder and I haven't stopped to figure out why this should be so, just went with what worked.

    As for moving the box or cube, I also will have a feature that will rotate the box in three directions -- haven't decided whether to use buttons or keys yet. I want to have a lot of different kinds of user input, but don't want it to be too complicated. That's why I'm trying to get the mouse to do what I want, so I don't have to add a keystroke to make the mouse move in either X or Y, though maybe I will have to do that.

    Thanks for all you help and suggestions. If you have any more after trying out my actual color cube, please let know.
     
  11. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    In the previous code examples I gave you, replace localEulerAngles with eulerAngles. Does that solve the issue?
     
  12. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky,

    The eulerAngles did remove the problem of my "snapping" script not working. Thanks! But unfortunately, it works like the example I tried above, of using Mathf.clamp with transform.Rotate. It will keep the mouse traveling in either X or Y direction but it's still easy to get things tilted because if the mouse is tilted when it starts rotating it will stay tilted.

    Isn't the camera object normally a child of the controller object?

    How did you do this? Did you attach a MouseLook script to both the controller and the camera with one set to X Axis and one set to Y axis? That didn't work for me. Thanks.
     
  13. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Odd, I'm not having the tilting problem. I created a brand new project and used this code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseLookTest : MonoBehaviour
    4. {
    5.     public float Sensitivity = 2.0f;
    6.     public float MoveSpeed = 0.2f;
    7.  
    8.     private float rotX;
    9.     private float rotY;
    10.  
    11.     private new Transform transform;
    12.  
    13.     void Awake()
    14.     {
    15.         transform = base.transform;
    16.  
    17.         rotX = transform.eulerAngles.y;
    18.         rotY = transform.eulerAngles.z;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         rotX += Input.GetAxis("Mouse X") * Sensitivity;
    24.         rotY -= Input.GetAxis("Mouse Y") * Sensitivity;
    25.  
    26.         transform.eulerAngles = new Vector3(rotY, rotX, 0.0f);
    27.  
    28.         transform.Translate((Input.GetAxis("Horizontal") * MoveSpeed) * Vector3.right + (Input.GetAxis("Vertical") * MoveSpeed) * Vector3.forward);
    29.     }
    30. }
    Do you intend to implement Player collision? Because if not, a Character Controller like that isn't necessary. The code like I wrote above could easily cover movement. But if you intend to add collision, then perhaps a Character Controller is in your best interest.

    Please note that you should disable your Character Controller component while testing this script, since it takes care of movement for you. You only need a single object with a camera attached for this script.

    Sorry if this is confusing :S
    Nope, I used one component and attached it to the camera and made the camera a child of the player GameObject. Here is what I did:

    Code (CSharp):
    1. float dx = Input.GetAxis("Mouse X");
    2. float dy = Input.GetAxis("Mouse Y");
    3.  
    4. Vector3 rotation = parent.localEulerAngles;
    5. rotation.y += dx;
    6. parent.localEulerAngles = rotation;
    7.  
    8. rotationY = Mathf.Clamp(rotationY + dy, MinY, MaxY);
    9.  
    10. rotation = transform.localEulerAngles;
    11. rotation.x = rotationY;
    12. transform.localEulerAngles = rotation;
     
    Zaffer likes this.
  14. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky, You're a genius! Your MouseLookTest script works. There is still some tilting, but it's controllable. I can use the mouse to straighten the image and get back to level. Whereas before, the scene would just spin out of control. I think my problem was that I didn't realize you didn't really need a controller script, that the MouseLook script alone would let you use the arrow keys to move. I had no idea. No, I don't need any type of collision, the only thing I will need to do is get my "snapping" code working with this new setup -- shouldn't be too difficult. Thanks so much!

    Zaffer
     
  15. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Franconte,

    I think my problem was that I didn't realize that I didn't really need a character controller script at all -- that the MouseLook script alone would move the object. Apparently the controller script was causing the tilt problem. With a modified MouseLook script using Mathf.clamp per Cranky's code, I can get things working well. Sorry I didn't realize your suggestion was a good one. Thanks.
     
  16. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
  17. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Fraconte likes this.
  18. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky,
    Famous last words.. I added my code for "snapping" -- positioning the camera along a line determined by two of the color spheres it only "sort of" works. When I play the scene and press the "t" key (input for this), the camera goes to the correct position and rotation, but when I release the "t" key, the camera stays in its new position but returns to its last rotation before the "t" key was pressed -- either the starting rotation or the last rotation determined by the user rotating the mouse (see attached). If you or anybody has a suggestion on how to fix this, I would greatly appreciate it. Thanks.
    Code (CSharp):
    1. void Update()
    2.     {      
    3.         rotX += Input.GetAxis("Mouse X") * Sensitivity;
    4.         rotY -= Input.GetAxis("Mouse Y") * Sensitivity;
    5.  
    6.         transform.eulerAngles = new Vector3(rotY, rotX, 0.0f);
    7.  
    8.         transform.Translate((Input.GetAxis("Horizontal") * MoveSpeed) * Vector3.right + (Input.GetAxis("Vertical") * MoveSpeed) * Vector3.forward);
    9.  
    10.         if(Input.GetButton("Test")){
    11.             PlaceCamera();
    12.         }
    13.     }
    14.  
    15.     void PlaceCamera()
    16.     {
    17.         obj1 = GameObject.Find ("255 255 255"); //first color sphere
    18.         obj2 = GameObject.Find ("0 0 0");        //second color sphere
    19.  
    20.         Vector3 startPoint = new Vector3(0, 0, 0);
    21.         startPoint = obj1.transform.position;
    22.         this.transform.position = startPoint;
    23.  
    24.         Vector3 direction = new Vector3(0, 0, 0);
    25.         direction = obj2.transform.position - obj1.transform.position;                          
    26.         Quaternion rot = Quaternion.LookRotation(direction);
    27.         this.transform.rotation = rot;
    28.     }
    Cranky_post.jpg
     
  19. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I lost my mind trying to understand why transform.eulerAngles reset the rotation. I also tryed to save and restoring old euler... I don't know why but it doesn't works.
    The good news is I have an easy fix for this:

    Code (CSharp):
    1.  
    2.         //rotX = Input.GetAxis("Mouse X") * Sensitivity;
    3.         //rotY = -Input.GetAxis("Mouse Y") * Sensitivity;
    4.  
    5.         //transform.eulerAngles = new Vector3(rotY, rotX, 0.0f);
    6.  
    Now for the rotation just add another script: the standard MouseLook.cs or the SmoothMouseLook.cs I told you above (or any other MouseLook you want...). :)
     
  20. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    This is because the MouseLook script is setting the transform's rotation to its own internal rotation value internally. The good news is this can be remedied pretty easily :).

    Add this to the end of your PlaceCamera() method:

    Code (CSharp):
    1. rotX = transform.eulerAngles.y;
    2. rotY = transform.eulerAngles.x
    Let me know how it goes!
     
    Zaffer likes this.
  21. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    It doesn't. It's my MouseLook script overwriting the eulerAngle values with rotX and rotY every frame :p
     
  22. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I can tell you... it didn't worked! :)
    I tryed exactly those lines :)
     
    Last edited: Aug 19, 2014
  23. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I don't see the problem: when T is pressed it set a new rotation and then your MouseLook continue from there...

    Edit: I tested your script and now I see the problem (too bad I still don't understand it...) :)

    Anyway I just tryed what I told and it works (at least with the standard MouseLook and SmoothMouseLook). The standard MouseLook use localEulerAngles and it works. I tryed to use that too on the script but with the same reset rotation effect as before. Standard MouseLook do something different from your code...
     
    Last edited: Aug 19, 2014
  24. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Cranky, You're a genius *again*. Your fix works perfectly. Thanks so much.
     
  25. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266

    I lost my mind too this morning trying to figure this out, but I got it back when Cranky's fix worked -- perfectly. Thanks for your efforts on my behalf, Fraconte.
     
  26. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Now I am really confused... :confused:
     
  27. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Fraconte,


    I will PM you with the whole script. Just attach it to the camera -- no parent object. It should work for you too.

    I can't find a way to PM on this forum and I tried to start a "conversation" with you, but got a dialog box saying I can't start a conversation with Fraconte. I didn't want to clutter up this thread with lots more code, but I'll go ahead and insert the whole script here. Hope it works for you. Let me know.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseLookTest_bp : MonoBehaviour
    4. {
    5.     public float Sensitivity = 2.0f;
    6.     public float MoveSpeed = 0.2f;
    7.    
    8.     private float rotX;
    9.     private float rotY;
    10.    
    11.     private new Transform transform;
    12.    
    13.     private GameObject obj1;
    14.     private GameObject obj2;
    15.  
    16.     void Awake()
    17.     {
    18.         transform = base.transform;
    19.         rotX = transform.eulerAngles.y;
    20.         rotY = transform.eulerAngles.z;
    21.     }
    22.    
    23.     void Update()
    24.     {   // mouse move code
    25.         rotX += Input.GetAxis("Mouse X") * Sensitivity;
    26.         rotY -= Input.GetAxis("Mouse Y") * Sensitivity;
    27.  
    28.         transform.eulerAngles = new Vector3(rotY, rotX, 0.0f);
    29.  
    30.         transform.Translate((Input.GetAxis("Horizontal") * MoveSpeed) * Vector3.right + (Input.GetAxis("Vertical") * MoveSpeed) * Vector3.forward);
    31.  
    32.         if(Input.GetButton("Test")){
    33.             PlaceCamera();
    34.         }
    35.     }
    36.  
    37.     void PlaceCamera()
    38.     {
    39.         // "snapping" code
    40.         obj1 = GameObject.Find ("255 255 255"); //first color sphere
    41.         obj2 = GameObject.Find ("0 0 0");        //second color sphere
    42.  
    43.         Vector3 startPoint = new Vector3(0, 0, 0);
    44.         startPoint = obj1.transform.position;
    45.         this.transform.position = startPoint;
    46.  
    47.         Vector3 direction = new Vector3(0, 0, 0);
    48.         direction = obj2.transform.position - obj1.transform.position;                          
    49.         Quaternion rot = Quaternion.LookRotation(direction);
    50.         this.transform.rotation = rot;
    51.  
    52.         rotX = transform.eulerAngles.y;
    53.         rotY = transform.eulerAngles.x;
    54.     }
    55. }
    56.  

    Zaffer
     
    Last edited: Aug 20, 2014
  28. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This is what I tryed (just little adaptations on obj1, obj2):
    Code (CSharp):
    1.  
    2. public class MouseLookTest : MonoBehaviour
    3. {
    4.     public float Sensitivity = 2.0f;
    5.     public float MoveSpeed = 0.2f;
    6.    
    7.     private float rotX;
    8.     private float rotY;
    9.    
    10.     private new Transform transform;
    11.    
    12.     void Awake()
    13.     {
    14.         transform = base.transform;
    15.        
    16.         rotX = transform.eulerAngles.y;
    17.         rotY = transform.eulerAngles.z;
    18.     }
    19.    
    20.     void Update()
    21.     {
    22.         rotX += Input.GetAxis("Mouse X") * Sensitivity;
    23.         rotY -= Input.GetAxis("Mouse Y") * Sensitivity;
    24.        
    25.         transform.eulerAngles = new Vector3(rotY, rotX, 0.0f);
    26.        
    27.         transform.Translate((Input.GetAxis("Horizontal") * MoveSpeed) * Vector3.right + (Input.GetAxis("Vertical") * MoveSpeed) * Vector3.forward);
    28.  
    29.         if(Input.GetButton("Jump")){
    30.             PlaceCamera();
    31.         }
    32.     }
    33.  
    34.     void PlaceCamera()
    35.     {
    36.         Vector3 obj1 = new Vector3 (100, 0, 100);//GameObject.Find ("255 255 255"); //first color sphere
    37.         Vector3 obj2 = new Vector3 (0, 0, 0);//GameObject.Find ("0 0 0");        //second color sphere
    38.        
    39.         Vector3 startPoint = new Vector3(0, 0, 0);
    40.         startPoint = obj1;//obj1.transform.position;
    41.         this.transform.position = startPoint;
    42.        
    43.         Vector3 direction = new Vector3(0, 0, 0);
    44.         direction = obj2 - obj1;//.transform.position - obj1.transform.position;                        
    45.         Quaternion rot = Quaternion.LookRotation(direction);
    46.         this.transform.rotation = rot;
    47.         rotX = transform.eulerAngles.x;
    48.         rotY = transform.eulerAngles.y;
    49.     }
    50.  
    51. }
     
  29. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Glad to hear it worked :). If you have any more questions, I'd be glad to help!
     
    Zaffer likes this.
  30. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Thanks again, I wish I could understand EulerAngles. They are obviously important. I just watched a math video on YouTube and I used to know some trig, but most of it went over my head. Evidently, y is related to pitch, x to yaw and z to roll which doesn't seem to agree with this quote from the Unity Manual Transform.eulerAngles page:
    . If I'm reading this correctly, it's saying that x is related to z, y to x and z to y. o_O

    What I'm actually trying to understand is why you used this code in the Awake function
    Code (CSharp):
    1. rotX = transform.eulerAngles.y;
    2. rotY = transform.eulerAngles.z;
    , but this code at the end of the PlaceCamera function
    Code (CSharp):
    1. rotX = transform.eulerAngles.y;
    2. rotY = transform.eulerAngles.x;
    Why is rotY related to eulerAngles.z int the first code and to eulerAngles.x in the second?

    I understand that I may be asking a really complicated question here. If your answer would involve a lot of work for you (like trying to teach me basic trig) please feel free to not answer :) Thanks.
     
  31. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Fraconte, did it work for you?
     
  32. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Of course not! That's why I am so surprised it works for you... :)
    What is different from your code (apart the obj1 obj2 adaptation)?
     
  33. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    It's actually really simple and you're over thinking it :p.

    Notice the three text boxes in Unity's inspector for rotation? Those are actually Euler angles! Y is associated with X because when you rotate on the Y axis, the object rotates left or right, which is the X axis. When you rotate on the X axis, the object looks up and down, which is the Y axis.

    It sounds confusing, but think of it as an airplane where Y = Yaw, X = Pitch, Z = Roll. Yaw is left and right, Pitch is up and down, and Z is like rotating, or rolling.

    Or think of the axis as a fixed column in which something could swing around on. For example, Y would be a column going up and down. If that column were to rotate, the object would be turning left or right.

    Hope that clears it up!
     
    Last edited: Aug 20, 2014
    Zaffer likes this.
  34. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    THAT'S IT! :)
    This was the difference: I was thinking rotX as rotation around X axis and rotY as rotation around Y axis while indeed they are mouse movements on axis X and Y. I think that confused you too in your euler understanding... :)
    This is what i wrote:
    Code (CSharp):
    1.  
    2. rotX = transform.eulerAngles.x;
    3. rotY = transform.eulerAngles.y;
    4.  
    After inverting x with y all works for me too!

    Would be better inverting all the rotX, rotY... just for clarity. :)
     
  35. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This is how I better understand it:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseLookTest_bp : MonoBehaviour
    5. {
    6.     public float Sensitivity = 2.0f;
    7.     public float MoveSpeed = 0.2f;
    8.    
    9.     float rotX;
    10.     float rotY;
    11.    
    12.     new Transform transform;
    13.    
    14.     GameObject obj1;
    15.     GameObject obj2;
    16.    
    17.     void Awake()
    18.     {
    19.         transform = base.transform;
    20.         rotX = transform.eulerAngles.x;
    21.         rotY = transform.eulerAngles.y;
    22.     }
    23.    
    24.     void Update()
    25.     {   // mouse move code
    26.         rotX -= Input.GetAxis("Mouse Y") * Sensitivity;
    27.         rotY += Input.GetAxis("Mouse X") * Sensitivity;
    28.        
    29.         transform.eulerAngles = new Vector3(rotX, rotY, 0.0f);
    30.        
    31.         transform.Translate((Input.GetAxis("Horizontal") * MoveSpeed) * Vector3.right + (Input.GetAxis("Vertical") * MoveSpeed) * Vector3.forward);
    32.        
    33.         if(Input.GetButton("Test")){
    34.             PlaceCamera();
    35.         }
    36.     }
    37.    
    38.     void PlaceCamera()
    39.     {
    40.         // "snapping" code
    41.         obj1 = GameObject.Find ("255 255 255"); //first color sphere
    42.         obj2 = GameObject.Find ("0 0 0");        //second color sphere
    43.        
    44.         Vector3 startPoint = new Vector3(0, 0, 0);
    45.         startPoint = obj1.transform.position;
    46.         this.transform.position = startPoint;
    47.        
    48.         Vector3 direction = new Vector3(0, 0, 0);
    49.         direction = obj2.transform.position - obj1.transform.position;                        
    50.         Quaternion rot = Quaternion.LookRotation(direction);
    51.         this.transform.rotation = rot;
    52.        
    53.         rotX = transform.eulerAngles.x;
    54.         rotY = transform.eulerAngles.y;
    55.     }
    56. }
    57.  
    Note: private is the default for Class members so you don't need it. Perhaps you can get rid of the Vector3(0, 0, 0) assignment on startPoint and direction too.
     
  36. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Thanks Cranky, I do tend to "over think." Your clear explanation is most helpful.
     
  37. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Glad you got it working Fraconte :)
     
  38. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Indeed, but I prefer set it explicitly. No difference, just personal preference.

    Yeah, there are tons of micro-optimizations you can make there. That's one of them ;).

    Glad to hear this is working for you guys now! Sorry that the naming scheme for rotX and rotY was so confusing! The X and Y in those names are referring to their respective Mouse Axis (MouseX = rotX, MouseY = rotY). It made more sense in my head to name them after the perceived direction you'd be looking.
     
  39. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Cranky,

    Just one more question (for now :)); why "rotY = transform.eulerAngles.z;" in Awake() and rotY = transform.eulerAngles.x; in PlaceCamera()? If rotY is associated with eulerAngles.z in Awake(), why is it associated with eulerAngles.x in PlaceCamera()? (I know eulerAngles.x is correct. I tried eulerAngles.z and it didn't work.) Just a detail I'm puzzled about. Thanks.

    I think I'm over the hump with getting the mouse motion to work. Your help has been invaluable. Now I just have to make about 250 more little gradients, decide what other user input to have, design a GUI etc etc. I'm sure you've been there :) I may be getting a bit ahead of myself, but when and if the project is finished, I'd like to include you in the credits. I'll let you know when I get that far.

    By the way, I'm not getting email notifications on this thread. I checked every checkbox I could find that requested email notification, but no luck -- still not getting them. So I will check back here once in a while.
     
  40. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Ah, well you see there is an amazingly elaborate and complicated reason for this!

    I messed up :X. Sorry, haha! That is a typo, and it should indeed be x in the Awake function.

    I'd love to see the finished product -- it seems like a very cool idea. It's not necessary to include me in the credits (unless you really want to, haha), as I'm just here to help out. I like to take breaks from my project and read the forums once in while.
     
  41. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    LOL