x


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.

more ▼

asked Mar 26 '10 at 12:02 AM

collypoo gravatar image

collypoo
25 2 2 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 26 '10 at 12:28 AM

Tuti gravatar image

Tuti
410 1 3 9

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.

Mar 26 '10 at 04:45 AM collypoo
(comments are locked)
10|3000 characters needed characters left

omg thank you very much, the smoothness of turning was perfect!

more ▼

answered Apr 14 '10 at 01:53 PM

Daneel gravatar image

Daneel
2 1 1 2

Yeah no problem. Just check the FPS tutorial. There are a bunch of nice stuff there: http://unity3d.com/support/resources/tutorials/fpstutorial Cheers

Apr 16 '10 at 08:39 PM Tuti
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2173
x1951
x208

asked: Mar 26 '10 at 12:02 AM

Seen: 9036 times

Last Updated: Mar 26 '10 at 12:02 AM