- Home /
Question by
Eslas · Nov 28, 2017 at 11:58 AM ·
unity 5scripting beginner
Rotate a GameObject slowly until reach some point
Hello!!
I have a gameObject in my scene that is rotating using this code transform.Rotate(Vector3.up * Time.deltaTime * 15f);
then I did another method that rotate the Object to be in front of my target using this code Object.transform.LookAt(target);
but what i wanted to do is make this move slowly so the user can see that rotation
thanks!!
Comment
Best Answer
Answer by Multirezonator · Nov 28, 2017 at 12:16 PM
public class Rotation : MonoBehaviour
{
private Quaternion _startRotation;
private Quaternion _targetRotation;
private float _rotationTime;
public Transform TargetObject; // you must to set the target object in the editor or code
public float TimeToRotation = 3f;
void Start()
{
_startRotation = transform.rotation;
transform.LookAt(TargetObject);
_targetRotation = transform.rotation;
transform.rotation = _startRotation;
_rotationTime = 0f;// reset it to zero if you want to start the new rotation
}
void Update()
{
_rotationTime += Time.deltaTime;
transform.rotation = Quaternion.Slerp(
_startRotation, _targetRotation, _rotationTime / TimeToRotation);
}
}
Your answer
