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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Why does this script cause the player to rotate?

Discussion in 'Scripting' started by OfficialHermie, Nov 30, 2018.

  1. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    hier1.jpg Hello!

    I don't understand what might make this script rotate the player at 45° at program start.

    This script is attached to the MainCamera which is parented to MyPlayer gameobject. The character model is attached to the MyPlayer object as well:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //Turn off this script from main camera if you're turned VR mode
    5. //rotates Main Camera based on mouse movements. Placed on Main Camera gameObject in the inspector
    6.  
    7. public class CameraRotation : MonoBehaviour
    8. {
    9.     public float speedH = 2.0f;
    10.     public float speedV = 2.0f;
    11.  
    12.     public float yaw = 0.0f;
    13.     public float pitch = 0.0f;
    14.  
    15.     void Start()
    16.     {
    17.         transform.eulerAngles = new Vector3(0, 0, 0.0f);
    18.     }
    19.  
    20.     void LateUpdate()
    21.     {
    22.         yaw += speedH * Input.GetAxis("Mouse X");
    23.         pitch -= speedV * Input.GetAxis("Mouse Y");
    24.         pitch = Mathf.Clamp(pitch, -20, 30);
    25.         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    26.     }
    27. }
    28.  
    Is there any chance that this script might rotate the player at program start, or do I need to look elsewhere for this strange behaviour?

    Thank you!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Your script applies global rotation.

    There is chance, that your reference parent object is rotated 45 degrees. Need check / confirm that. Which in result, the outcome may looks like having 45 deg offset, while in fact, it sets to 0 rotation.
     
  3. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    @Antypodish Could you tell me how to make it so that it takes the initial rotation of the parent object into account? I guess that is indeed the culprit as myPlayer is rotated (Y rotation = -180). Or do you think there's something akward about the way I do it?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    You need to apply localRotation, instead eulerAngles, which modifies global rotation in this case.
    There is few ways of doing so.
    But just to follow your nearest approach
    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler ( new Vector3 () ) ;
    Hence
    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler ( new Vector3 ( pitch, yaw, 0.0f ) ) ;
    I think that should work.
     
  5. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    Thank you for the explanation! I have made the changes, and the character doesn't rotate "globally" anymore, but the behaviour is different now. I have recorded a video
    . The first part shows how the mouse move directs the player / camera before the modification. The second part shows how the mouse move directs the player / camera after the modification. There is a huge difference. I can't see why, but perhaps you can see that right away?
    I would very much like these movements as seen in the first part.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    I think this is rotation axis ordering issue.

    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler ( Vector3.up * yaw ) * Quaternion.Euler ( Vector3.right * pitch ) ;
    Basically you first want to apply vertical yaw rotation and then add horizontal pitch rotation.
    Is kind of like adding quaternions (local rotations in respect to own (local) axis).

    I haven't tested it.
    So try that and come back, if having further issues.
     
  7. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    Thank you, but now it keeps rotating. Would you like to see a video?
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Nope I think I know why.

    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler ( transform.parent.up * yaw ) * Quaternion.Euler ( transform.parent.right * pitch ) ;
    This assumes, that you don't manipulate object transform.rotation of rotator.

    I hope this works :p
    Let me know.
     
  9. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    Now it rotates in some other way. :)
    I didn't apply any rotation on the "rotator" game object.
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Does it keeps rotating?

    Eh I don't have rig to test atm.

    Try this.
    Code (CSharp):
    1. transform.rotation = transform.parent.rotation * Quaternion.Euler ( Vector3.up * yaw ) * Quaternion.Euler ( Vector3.right * pitch ) ;
    Which is the same as
    Code (CSharp):
    1. transform.rotation = transform.parent.rotation * Quaternion.Euler ( 0, yaw, 0 ) * Quaternion.Euler ( pitch, 0, 0 ) ;
    Check individually also
    Code (CSharp):
    1. transform.rotation = transform.parent.rotation * Quaternion.Euler ( Vector3.up * yaw ) ;
     
    Last edited: Nov 30, 2018
  11. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    Unfortunately it keeps rotating.
     
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    I have tested these last 3 examples, with 2 cubes, for witch, one is parent, other is a child.

    So the error must be somewhere else.
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    This definitely works.

    Code (CSharp):
    1.  
    2. [code=CSharp]using UnityEngine;
    3.  
    4. public class Script : MonoBehaviour
    5. {
    6.     float yaw = 0 ;
    7.     float pitch = 0 ;
    8.  
    9.     void LateUpdate ()
    10.     {
    11.         yaw += 2 * Input.GetAxis("Mouse X");
    12.         pitch -= 2 * Input.GetAxis("Mouse Y");
    13.         pitch = Mathf.Clamp(pitch, -20, 30);
    14.  
    15.         transform.rotation = transform.parent.rotation * Quaternion.Euler ( Vector3.up * yaw ) * Quaternion.Euler ( Vector3.right * pitch ) ;
    16.     }
    17. }
    [/code]

    However, while following is not source of problem, you should really have inputs in Update, rather LateUpdate.
     
  14. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
  15. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Right, I checked quickly. And realizing, that rotator actually rotates, as well as moves. Is the target for a camera.
    Then seams there is more into the rotation script, some lerping somewhere etc.

    Sorry, but I stopped there. I am not going spend time studying whole system.
    Since I haven't investigated further, I don't know what affect rotation. There are other scripts as well ...

    You need to drill down, to find out, what affects what.
    If not understanding, I suggest go decompose to smaller pieces and learn, what does what.

    Or you may need go back to basics.
     
  16. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    Ok, thank you very much!