Search Unity

C# Rotation Script

Discussion in 'Scripting' started by orionburcham, Feb 23, 2011.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Can someone please show me a C# script that rotates a GameObject from 0 to 90 degrees on y over 5 seconds? I'm having trouble writing this myself, even though it seems so simple.

    Thanks!

    P.S. using SmoothDampAngle to smooth out the rotation would also be helpful, if you want to. Gracias.
     
    Last edited: Feb 23, 2011
  2. erune

    erune

    Joined:
    Nov 14, 2009
    Posts:
    63
    .
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Thanks! Though I'm having trouble getting that to work. There are a few typos, missing arguments, etc, and I'm having trouble seeing where the scope tabbing would be. I must not be filling in the blanks how you expected. Would you mind fleshing it out just a little more, possibly in code tags?

    Seriously appreciated, though. Here was my attempt to use what you wrote:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RotationScript : MonoBehaviour
    6. {
    7.  
    8.     public void Start()
    9.     {
    10.         StartCoroutine("Rotate90");
    11.     }
    12.  
    13.             public IEnumerator Rotate90()
    14.             {
    15.                 Vector3 axis = Vector3.up;
    16.                 float angle = 90.0f;
    17.                 Quaternion desiredRotation = Quaternion.AngleAxis(angle, axis);
    18.                 Quaternion startingRot = transform.rotation;
    19.  
    20.                 while (Quaternion.Angle(transform.rotation, desiredRotation) > 0.1f)
    21.                 {
    22.                     transform.rotation = Quaternion.Slerp(startingRot, desiredRotation, Time.deltaTime);
    23.                     yield return 0;
    24.                 }
    25.             }
    26.         }
    27.  
    28.  
     
  4. erune

    erune

    Joined:
    Nov 14, 2009
    Posts:
    63
    My mistake. I have forgot to apply axis-angle rotation to get the final rotation.

    Try this instead. Must also work faster than the previous method. I am not able to try the script right now. Please write down if there are any mistakes.

     
  5. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Thanks a lot! It appears to work fine- any chance you could make it stop at exactly 90 degrees? (right now it rotates forever) That's my bad, I should have been clearer about that.

    All my attempts just catch it after the y is above 90, which is never exact- I'm hoping to learn from someone better. Of course only if you want to. Thanks again.

    (More Specific):

    Could the rotation stop exactly at any given degree? Not just after a number of seconds, but, say, exactly at 45 degrees every time, for example? I understand if this is too much work to ask for here.
     
    Last edited: Feb 23, 2011
  6. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    You know, looking at your code, it seems like it should stop rotating at or around 90 degrees (when rotAmount > angle). I'm not sure why it doesn't, actually..
     
  7. erune

    erune

    Joined:
    Nov 14, 2009
    Posts:
    63
    This one is tested
     
  8. tapticc

    tapticc

    Joined:
    Jan 16, 2014
    Posts:
    379
    erune thank you I have been trying for a couple of days to sort this "simple" one out! Only thing I would add is to watch out for the public variables. I was testing this and it was slow, so I increased the speed variable in code right up to 2000 and was same speed. This was because the variable was public and so was attached to the inspector on the first run, which then "stuck" at 1.5. I made this private in my code to sort it, but I think only noobs like me will fall for that one lol

    thanks again, really helped me out
     
  9. youtubevirus

    youtubevirus

    Joined:
    Feb 22, 2018
    Posts:
    1
    i dont know guys why are you doing this so difficult just do this:

    public gameobject box;


    box.transform.rotate(0,-90,0);

    and thats all :D