Hi Unity Anwsers,
I’m trying to make a C# script that’ll rotate a GameObject 90 degress og -90 degress on the y-axis over 1 second when I press a button. I’ve searched around, but I can’t find anything that works.
So far, I’ve made a float that holds rotation, and set it to 90f. Then I’ve put a transform.Rotate in my ‘update’ function when I press the keys ‘e’ or ‘q’ However, this turns the object immediately and not over time - can anybody help med add the time to my code?
public float rotation = 90f;
void Update () {
if (Input.GetKeyDown ("e")) {
transform.Rotate (0, rotation, 0);
}
if (Input.GetKeyDown ("q")) {
transform.Rotate (0, -(rotation), 0);
}
}
3 Answers
3
Thank you so much @Mmmpies.
That did the trick! My final code ended up looking like this:
IEnumerator RotateMe(Vector3 byAngles, float inTime)
{ var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for(var t = 0f; t < 1; t += Time.deltaTime/inTime) {
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
yield return null;
}
}
void Update () {
if(Input.GetKeyDown("e")){
StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
}
if(Input.GetKeyDown("q")){
StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
}
}
}
i know this an old post but it helped me and will help others i altered it a bit to stop you rotating till it had finished or you could get odd angles
bool rotating;
IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for (var t = 0f; t <= 1; t += Time.deltaTime / inTime)
{
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
yield return null;
}
transform.rotation = toAngle;
rotating = false;
}
void Update()
{
if (Input.GetKeyDown("e") && !rotating)
{
rotating = true;
StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
}
if (Input.GetKeyDown("q") && !rotating)
{
rotating = true;
StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
}
}
I noticed as I run this code the object (cube in my case) doesn’t rotate a complete 90 degrees. It falls short on each key press. Any idea why?
There are a few ways to accomplish this. I would recommend you checking out [Quaternion.Lerp()][1] or Trying to rotate it based on Time.deltaTime() like this: void Update() { if (Input.GetKeyDown ("e")) { transform.Rotate(0, rotation * Time.deltaTime, 0); } if (Input.GetKeyDown ("q")) { transform.Rotate(0, -(rotation) * Time.deltaTime, 0); } } [1]: http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
– NomabondThanks @Nomabond, I'm really, really new to C# and Unity, so I still need some help setting it up. What do I do with 'Transform from' and 'Transform to' to make it spin 90 degress on the keypress? public Transform from; public Transform to; public float rotation = 90f; void Update () { if (Input.GetKeyDown ("e")) { Quaternion.Lerp(from.rotation, to.rotation, Time.time * rotation); }
– RullaghnNo need to reinvent the wheel (even if you want smooth rotation!) http://answers.unity3d.com/questions/672456/rotate-an-object-a-set-angle-over-time-c.html That co-routine will do it, you don't even need it that complex but copy and paste it and send it the values 90 or -90 and 1 for the duration.
– Mmmpies