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. Dismiss Notice

Rotation of Gameobject doesn't work

Discussion in 'Scripting' started by inicosia99, Nov 26, 2020.

  1. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    Hi, I'm working on 2 projects, both involve the rotation of a gameobject.

    In the first case, I need to rotate the camera of 45 degrees on the z-axis if the player types right and -45 degrees is he types left.

    in the second one is a platform that I need to rotate. 90 degrees if the player types right and -90 on the other case. The codes are practically the same but in the second case, the platform rotates but not in a correct way. Instead of doing 90, 180, 270, 360, It does 90,-180,-90,0....


    Please help me cause I tried everything.

    the code is this one

    Code (CSharp):
    1. public Transform Coso;
    2.     public Vector3 angle;
    3.     Rigidbody rb;
    4.     public int sinistra = 1;
    5.  
    6.     private void Start()
    7.     {
    8.         sx = false;
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         rb.velocity = new Vector3(0, 0, 4*Time.deltaTime);
    15.     }
    16.    
    17.     void LateUpdate()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             if (Input.mousePosition.x < Screen.width / 2)
    22.             {
    23.                 sinistra--;
    24.                 Indici();
    25.             }
    26.             else if (Input.mousePosition.x > Screen.width / 2)
    27.             {
    28.                 sinistra++;
    29.                 Indici();
    30.             }
    31.         }
    32.  
    33.     }
    34.  
    35.     void Indici()
    36.     {
    37.  
    38.         if (sinistra == -1)
    39.             sinistra = 3;
    40.         else if (sinistra == -2)
    41.             sinistra = 2;
    42.         else if (sinistra == -3)
    43.             sinistra = 1;
    44.         else if (sinistra > 3)
    45.         {
    46.             if (sinistra == 4)
    47.                 sinistra = 0;
    48.             else if (sinistra == 5)
    49.                 sinistra = 1;
    50.             else if (sinistra == 6)
    51.                 sinistra = 2;
    52.             else if (sinistra == 7)
    53.                 sinistra = 3;
    54.         }
    55.  
    56.         if (sinistra == 0)
    57.             Coso.Rotate(0, 0, 0, Space.World);
    58.         else if (sinistra == 1)
    59.             Coso.Rotate(0, 0, 90, Space.World);
    60.         else if (sinistra == 2)
    61.             Coso.Rotate(0, 0, 180, Space.World);
    62.         else if (sinistra == 3)
    63.             Coso.Rotate(0, 0, -90, Space.World);
    64.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This appears to be a duplicate post: https://forum.unity.com/threads/proble-with-3d-gameobject-rotation.1012099/

    Let me say it again.

    DO NOT USE the .Rotate() method. That is RELATIVE.

    Instead, you must track your own notion of angle (and you set it to -45, 0 or 45) and then directly assign that angle to the rotation.

    Code (csharp):
    1. transformToRotate.rotation = Quaternion.Euler( 0, 0, angle);
    NOTE: the above transform should be the transform that WILL ROTATE THE CAMERA. This should be obvious.

    Honest, it will work. This is how to to do it. If you do it and it doesn't work for you, there is another problem.

    If this is the case, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    I'm searching everywhere but transformToRotate.rotation doesn't exist.
     
  4. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    the proble here is that the camera doesn't rotate. My code works fine with other gameobjects but not with the cam
     
  5. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    found the solution. wasn't the script. but in universal render pipeline there is a script attached to the cam that doesn't consent the movement.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    That's why you ALWAYS want to start a new scene and test your code, if at all possible. That way you know why it is failing. Animation always wins out over scripting movement.

    That is the field that YOU would declare that points at the camera.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // Put this in a new default scene, drag the camera transform to transformToRotate, and press PLAY
    4. // left/right arrow will instantaneously tilt the camera
    5.  
    6. public class CameraTiltingExample : MonoBehaviour
    7. {
    8.     public Transform transformToRotate;  // you need to drag something into this!
    9.  
    10.     void Update ()
    11.     {
    12.         float desiredAngle = 0;
    13.  
    14.         if (Input.GetKey( KeyCode.LeftArrow))
    15.         {
    16.             desiredAngle = 45;
    17.         }
    18.         if (Input.GetKey( KeyCode.RightArrow))
    19.         {
    20.             desiredAngle = -45;
    21.         }
    22.  
    23.         transformToRotate.rotation = Quaternion.Euler (0, 0, desiredAngle);
    24.     }
    25. }
    Or for example, if you want it to be smooth:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // Put this in a new default scene, drag the camera transform to transformToRotate, and press PLAY
    4. // left/right arrow will smoothly tilt the camera
    5.  
    6. public class CameraTiltingSmoothly : MonoBehaviour
    7. {
    8.     public Transform transformToRotate; // you need to drag something into this!
    9.  
    10.     float currentAngle;
    11.     const float tiltSpeed = 100;
    12.  
    13.     void Update ()
    14.     {
    15.         float desiredAngle = 0;
    16.  
    17.         if (Input.GetKey( KeyCode.LeftArrow))
    18.         {
    19.             desiredAngle = 45;
    20.         }
    21.         if (Input.GetKey( KeyCode.RightArrow))
    22.         {
    23.             desiredAngle = -45;
    24.         }
    25.  
    26.         currentAngle = Mathf.MoveTowards( currentAngle, desiredAngle, tiltSpeed * Time.deltaTime);
    27.  
    28.         transformToRotate.rotation = Quaternion.Euler (0, 0, currentAngle);
    29.     }
    30. }