- Home /
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
Answer by Eric5h5 · Aug 05, 2010 at 06:59 AM
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.
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
Your answer
Follow this Question
Related Questions
Issue with Collisions due to Parenting script 0 Answers
Aim only works while button is held down... how do I make it a toggle? 1 Answer
Row Boat script 0 Answers
How do i attach this script to my player? 1 Answer
movement 2d in a grid 3 Answers