- Home /
How do I make my Game Object face the direction it's going?
Hi Guys. I'm new to Unity 3D. I've been following the lessons from www.unity3dstudent.com and I've been trying to make my object move using the keyboard. I'm having trouble making it face the direction it's going. Any help and advice would be greatly appreciated. Thanks in advance to anyone who can help me. :)
Here is the script I've been using:
var speed = 3.0;
var rotateSpeed = 3.0;
function Update () {
var horiz : float = Input.GetAxis("Horizontal");
transform.Translate(Vector3(horiz * rotateSpeed,0,0));
transform.Rotate(Vector3(0,horiz * rotateSpeed,0));
{
var verti : float = Input.GetAxis("Vertical");
transform.Translate(Vector3(0,0,verti * speed));
}
}
This is what I would have done.
You will have to attach a $$anonymous$$ouseLook Script to your Camera and also attach some script to follow your Object. And stop Rotating your transform and ins$$anonymous$$d get the relative direction in which it has to move compared to your camera.
OR
You have to parent your object with some other object. and move your parent object around, but apply rotation to your child object. For applying rotation, you could look into Lerp() and Slerp() functions.
That's totally what I feel and would have done.
Answer by robertbu · Feb 12, 2014 at 01:53 PM
Your problem is line 5. You don't want to use the 'Horizontal' axes for both translation and rotation. Line 10 handles the translation.
In addition, this code should scale things by Time.deltaTime so that it runs consistently as the Frames per Second change.
#pragma strict
var speed = 3.0; // Units per second
var rotateSpeed = 90.0; // Degrees per second
function Update () {
var horiz : float = Input.GetAxis("Horizontal");
transform.Rotate(Vector3(0,horiz * rotateSpeed * Time.deltaTime,0));
var verti : float = Input.GetAxis("Vertical");
transform.Translate(Vector3(0,0,verti * speed * Time.deltaTime));
}
Answer by VTW · Feb 12, 2014 at 01:56 PM
First, move your var horiz and verti declarations above where your speed and rotateSpeed are. This way you won't keep instantiating a new variable every time your program updates. Instead set horiz within update, or not at all.
You could get rid of horiz and verti and just have "transform.Translate(Vector3(Input.GetAxis("Horizontal") * rotateSpeed....."
As far as rotation goes, what I did (C#) is check if the Input.GetAxis("Horizontal") was > 0. If it was, player was moving right. Else, the player was moving left. You'll want to rotate on the Y-axis via transform.Rotate() within each condition, as that will make your player spin to face another direction.
Answer by MikeNewall · Feb 12, 2014 at 01:59 PM
You need to translate in the forward direction of your characters transform.
transform.Translate(transform.forward * verti * speed);
@$$anonymous$$ikeNewall - Translate() takes local directions, so passing it a world direction like transform.forward will not work unless you add the Space.World parameter to the Translate().