x


Face forward direction of movement

I am trying to get an object like an arrow to point in the direction it is moving. I am using this code from the Roll a Ball tutorial. Any ideas?

public var force:float = 1.0;
public var simulateAccelerometer:boolean = false;

function Start()
{

    iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape;
}

function FixedUpdate () {
    var dir : Vector3 = Vector3.zero;

    if (simulateAccelerometer)
    {

        dir.x = Input.GetAxis("Horizontal");
        dir.z = Input.GetAxis("Vertical");
    }
    else
    {

        dir.x = -iPhoneInput.acceleration.y;
        dir.z = iPhoneInput.acceleration.x;


        if (dir.sqrMagnitude > 1)
            dir.Normalize();
    }

    rigidbody.AddForce(dir * force);
}

Thanks

more ▼

asked Feb 09 '11 at 07:36 AM

Richard 3 gravatar image

Richard 3
424 46 50 61

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

2 answers: sort voted first

Use this:

transform.rotation = Quaternion.LookRotation(dir);

Note that if you make the object always face in the direction of movement this way, it won't be rolling anymore (of course) so that may affect the physics if you were relying on a rolling behaviour.

You could instead still have a round collider that is rolling, and have your visible object be a child object of that, and use the LookRotation function on that child, so the visible child (without a collider) is pointing in the direction of movement while the invisible parent (with a collider) is rolling freely.

Edit: Actually, you may want to use this to avoid very fast rotations, or errors if the object is standing still:

if (dir != Vector3.zero) {
    transform.rotation = Quaternion.Slerp(
        transform.rotation,
        Quaternion.LookRotation(dir),
        Time.deltaTime * rotationSpeed
    );
}
more ▼

answered Feb 09 '11 at 12:09 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

This is exactly what I needed. Thanks

Feb 09 '11 at 02:04 PM Richard 3

Is there anyway to maintain it's last faced direction even after no Input?

Jul 18 '11 at 06:30 AM pikpiak
(comments are locked)
10|3000 characters needed characters left

Here's another solution that will look in the direction the ball's moving, not the direction of input. Good for attaching a hat/gun to the ball or something.

transform.position = ballGameObject.transform.position;
transform.LookAt(transform.position + ballGameObject.rigidbody.velocity);
  • Position this object to where the ball is.
  • Add the ball's "rigidbody.velocity" to this object's position, and then look at it. (The velocity value gives a point offset from the center that you can look at.)

Now just make your arrow model a child of this GameObject. Move the Z position out as far as you want the arrow to appear.

more ▼

answered Jan 20 at 09:48 AM

Stardog gravatar image

Stardog
255 21 29 36

(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:

x1427
x302
x296
x255
x48

asked: Feb 09 '11 at 07:36 AM

Seen: 3908 times

Last Updated: Jan 20 at 09:49 AM