- Home /
How do i get smooth rotation to a point in space
Sorry if someone has already answered this. I've looked and haven't found anything like whats happening to me.
I have a point in space (Vector3) that I want the object to smoothly turn to but I cant seem to make the code work:
function RotateTowardsPosition (targetPos, rotSpd ) { // Convert the vector2 to a vector3 var target = (Vector3(targetPos[0], 0, targetPos[1])); // get the current position var currentPos = transform.position; var relativePos = target - currentPos ; var rotation = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.time * rotSpd);
}
When I run this I get an error stating the the "Object reference not set to an instance of and object" and is states the var target line as where the error starts. Any help would be much appreciated.
Answer by Tuti · Mar 26, 2010 at 12:28 AM
Try this:
var targetPosition : GameObject;
function Update () {
// Smoothly rotates towards target
var targetPoint = targetPosition.transform.position;
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
}
Hope that helps.
Sorry about that... I figured it out. It was that variable that I was using. I didn't have it outside the Update function so it kept getting overwritten as a null which was causing the error. Thank you for the help, your script works perfectly.
Your answer
Follow this Question
Related Questions
Smooth rotation without using an update()? 1 Answer
Smooth Rotation 2 Answers
Smooth 2D rotation unexpected 1 Answer
Quaternion to Matrix Conversion Error - Camera Script 0 Answers