- Home /
Rotate to Y degrees over X amount of time in .cs
Its in the question, Im new jsut started today if any one can help
Well what i actually need is to have the character turn to lets say 0° when i press D and to turn to 180 when i press A and 270 when i press W, I tired several things but im too new to understand what im doing wrong.
Answer by robertbu · Jul 21, 2013 at 06:30 PM
Here is a solution to your updated question:
using UnityEngine;
using System.Collections;
public class Bug20 : MonoBehaviour {
Quaternion qTo;
float speed = 90.0f;
void Start() {
qTo = transform.rotation;
}
void Update () {
if (Input.GetKeyDown(KeyCode.A))
qTo = Quaternion.LookRotation(Vector3.left);
else if (Input.GetKeyDown(KeyCode.D))
qTo = Quaternion.LookRotation(Vector3.right);
else if (Input.GetKeyDown(KeyCode.W))
qTo = Quaternion.LookRotation(Vector3.forward);
else if (Input.GetKeyDown(KeyCode.S))
qTo = Quaternion.LookRotation(Vector3.back);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
}
}
'speed' is degrees per second. If you want a more eased movement, replace 'RotateTowards' with 'Slerp' and reduce the speed variable (perhaps to around 5).
Looks like a working solution to me. A few more details for the benefit of our new friend, some of which you might want to add your code.
The code you see above @Dincicode must go in a script like this:
using UnityEngine;
public class $$anonymous$$yClassName : $$anonymous$$onoBehaviour
{
// the code above
}
This script must then be attached to the GameObject you want to rotate, i.e. a character. Be careful that if you already have something else controlling the character i.e. a Character Controller, there might be conflicts as both scripts try to tell the character what to do.
If you are not into mathematics don't bother spending time trying to understand what Quaternions are: they can be obscure but they work well without having to look under the hood. In this case @robertbu first creates a "destination rotation" with the method Quaternions.LookRotation() and then uses the method Quaternions.RotateTowards() to rotate the object toward that destination at a given fixed speed per second.
Hope this helps!
@manu3d - I missed that he wanted it in C#. The original code was Javascript and needed more than just a class enclosure to work. I've edited the answer so the code is C#.
Actually, I have no idea which language he wanted the answer in but it appears he liked your answer anyway. =)
By the way, what is the using System.Collections for? And why aren't Start() and Update() declared as public?
Answer by IgorAherne · Jul 21, 2013 at 07:34 AM
this.transform.Rotate(0,20,0);//rotate 20 units every second, put this line in the FixedUpdate(){}
"this" refers to a script that is attached to some gameObject (Or has to be attached at least).
We want to access the Transform component from the current Script component, so we use a default variable "transform".
transform allows us to use different functions to alter itself, Rotate() is one of them.
@IgorAherne - your suggested code will not work correctly if it is in FixedUpdate(). It works in Update(). Not sure, but I think the person asking the question wants the object to rotate the specific amount and then stop rotating at that angle.
@IgorAherne: removing deltaTime won't work either. You could replace Time.deltaTime in your original code with Time.fixedDeltaTime, and it will work. But the rotation code does not belong in FixedUpdate(). The "right" solution is to use your original code in Update(). Typically FixedUpdate() is used for Physics, but nothing here uses the Physics engine.