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

Just a simple smooth left rotation of 90 degrees is making me crazy!

Discussion in 'Scripting' started by Yebbes, Sep 19, 2020.

  1. Yebbes

    Yebbes

    Joined:
    Jul 16, 2020
    Posts:
    18
    Hi All,

    I would like to smoothly turn a gameobject -90 degrees (left) or 90 degrees (right).
    I have read a lot of forums and articles, but all does not work with me? I am not getting it and writing and trying for like 8 hours straight to get this simple task working :)

    I tried Vector3, I tried Quaternion-eular-rotation stuff... it seems there is nothing "easy" to make a gameobject rotate -90 degrees (over it's Y-axis) and set a boolean to false when that task was completed.?

    Please help :)

    I added a nice image of my game and a cat, because we all love them.


    ...
    private speedRotation = 5 // speed
    private turnLeft = true; // test turning left
    void Update(){

    if (turnLeft){
    skullEnemy.transform.Rotate(new Vector3(0,speedRotation,0)); // rotates
    if (skullEnemy.transform.eulerAngles.y == -90){
    turnLeft = false; // complete
    }
    }

    }
     

    Attached Files:

    Last edited: Sep 19, 2020
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Code (CSharp):
    1. Vector3 rotation = new Vector3(0, 0, 0);
    2. float speed = 100;
    3. float y = 0;
    4. void Update()
    5.     {
    6.         if(turnLeft)
    7.         {
    8.             y -= speed * Time.deltaTime;
    9.             if (y <= -90)
    10.             {
    11.                 turnLeft = false;
    12.                 y = -90;
    13.             }
    14.             rotation = new Vector3(0,y,0);
    15.             skullEnemy.transform.eulerAngles = rotation;
    16.         }
    17.     }
     
  3. Yebbes

    Yebbes

    Joined:
    Jul 16, 2020
    Posts:
    18