x


facing direction that does not return to zero

I am new to Unity but still a very experienced Python coder and Maya Animator so forgive me if I ask stupid questions in UnityScript. :)

I am trying to develop a controller for moving the character around. Think of the set-up as controlling a radio controlled car and watching it from above, looking strait down at it. In the game the player moves the character along the ground along x and z axis. The camera, for now, is simply moved up 20 units and looking strait down at the character.

I can move the character around with a simple control script like the following:

var speed = 10;

function Update () {
    //print("UPDATING");

    var horizontalInput = Input.GetAxis("Horizontal");
    var verticalInput = Input.GetAxis("Vertical");

    var xPos = horizontalInput * Time.deltaTime * speed;
    var zPos = verticalInput * Time.deltaTime * speed;

    transform.Translate( xPos, 0, zPos );
}

What I can't figure out is how do I steer the character in the direction of the motion? I've seen a couple of answers for something similar that I have tried but they all have one undesirable result, they return the character back to facing at it's origin (0 degrees north) when the motion stops. The problem I am running into is when the player stops, the car shouldn't magically return to facing up the Z-Axis, it should stay facing in the last direction it was moving in.

Also, I am applying the script to a generic GameObject with the geometry for the car parented underneath it. Should I be using a CharacterController instead?

I hope this all makes sense and any help you can spare would be greatly appreciated.

Thanks, Grant

more ▼

asked Aug 05 '10 at 05:48 AM

Grant 2 gravatar image

Grant 2
26 5 5 7

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

1 answer: sort voted first
var speed = 10.0;

function Update () {
    var xPos = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    var zPos = Input.GetAxis("Vertical") * Time.deltaTime * speed;

    transform.position.x += xPos;
    transform.position.z += zPos;

    var move = Vector3(xPos, 0.0, zPos);
    if (move != Vector3.zero) {
        var rotation = transform.rotation; 
        rotation.SetLookRotation(move); 
        transform.rotation = rotation;
    }
}

You're better off using "var speed = 10.0" or "var speed : float = 10", because otherwise speed is limited to integers (it's also a teensy bit slower, since the int has to be converted to a float every time it's used).

I changed the movement so it's setting the transform.position directly instead of using Translate. Since Translate is relative, the movement wouldn't work the same anymore when you change the heading.

more ▼

answered Aug 05 '10 at 06:59 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Thanks Eric. I'll give it a try and see how it works out. Thanks for the tip on the whole float/int conversion, it's good to know those little details especially with performance.

To understand the code without trying it, in this case you just capture the rotation of the object's tranform, use the "SetLookRotation" method to explicitly set the direction to the heading of the vector and then re-apply it back to the transform's rotation. Seems simple enough.

I think all the solutions using "lookAt" methods was tripping me up.

Thanks again, Grant

Aug 06 '10 at 10:17 PM Grant 2
(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:

x3419
x1422
x104
x48

asked: Aug 05 '10 at 05:48 AM

Seen: 942 times

Last Updated: Aug 05 '10 at 05:48 AM