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

proble with 3d gameObject rotation

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

  1. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    Hi ! So I have a probelm wit this code. I want to rotate of +90 degrees the prefabs if I type right and -90 degrees if I type left.

    the problem is that it doesn't work properly cause for eg I type only right it does : 90 degrees then 180 the -90

    here it is the code :
    Code (CSharp):
    1.  public GameObject prefab;
    2.     public float angle = 0f;
    3.     public bool sx = false;
    4.     Rigidbody rb;
    5.  
    6.     private void Start()
    7.     {
    8.         sx = false;
    9.         rb = GetComponent<Rigidbody>();
    10.         angle = prefab.transform.eulerAngles.z;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         rb.velocity = new Vector3(0, 0, 4*Time.deltaTime);
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.             if (Input.mousePosition.x < Screen.width / 2)
    19.             {
    20.                 sx = true;
    21.                 angle -= 90.0f;
    22.             }
    23.             else if (Input.mousePosition.x > Screen.width / 2)
    24.             {
    25.                 sx = false;
    26.                 angle += 90.0f;
    27.             }
    28.             prefab.transform.Rotate(new Vector3(0, 0, angle), Space.Self);
    29.         }
    30.     }
    Thank you so much for the help
    irene :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Instead of using .Rotate(), which is relative, instead assign the rotation:

    Code (csharp):
    1. transformToRotate.rotation = Quaternion.Euler( 0, angle, 0);
    Then change
    angle
    and call the above.

    ALSO: remember if you change the rotation of a prefab on disk, you are changing what is on disk. This is usually NOT what you intend.

    If you want to change what is in the scene, get a reference to what you Instantiate (it is returned by Instantiate) and operate on that object instead.

    If the thing you are calling
    prefab
    already exists in the scene, I highly recommend renaming the variable to something else because that is going to be very misleading. Prefabs should only refer to objects on disk, just for clarity.
     
    Last edited: Nov 26, 2020
    Bunny83 likes this.
  3. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90

    Hi thank you so much the reply but I had already tried transform.rotation but the result is the same
     
  4. Lance2186

    Lance2186

    Joined:
    Sep 9, 2014
    Posts:
    8
    One other issue is that you are adding or subtracting 90 degrees every time you click a mouse button, then applying that total value to your rotation. So if you start at 0, one click will go to -90. If you click the same button again, it becomes -180. Then, -270 and so on. So instead of subtracting only another 90, you are applying a -270 rotation to the object.

    Instead of incrementing angle (angle -= 90f), try applying your rotation code with the + or - 90 value inside those mouse mousePosition.x blocks, instead of after.


    Code (CSharp):
    1.  
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             if (Input.mousePosition.x < Screen.width / 2)
    5.             {
    6.                 sx = true;
    7.                 prefab.transform.rotation = Quaternion.Euler(0f, -90f, 0f);
    8.             }
    9.             else if (Input.mousePosition.x > Screen.width / 2)
    10.             {
    11.                 sx = false;
    12.                prefab.transform.rotation = Quaternion.Euler(0f, 90f, 0f);
    13.             }
    14.         }
     
  5. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    thanks but it has to rotate of 360 on z axis