- Home /
Rotation and movement?
Hey guys. I'm still learning Unity, using JavaScript. I'm trying to make a character that you rotate with the left, and right, arrow keys, and then you press up, and he moves forward. I kind of have it working, but not quite. The problem is, once you rotate around so much, then the character starts moving in a different direction, than he is facing. I don't really know how to explain it better than that, other than the direction he is moving, and the direction he is facing, is not matching up. Here's the code
#pragma strict
var walkSpeed : int;
var runSpeed : int;
var turnSpeed : int;
function Update ()
{
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(0,-turnSpeed * Time.deltaTime * turnSpeed,0);
}
if (Input.GetKey(KeyCode.RightArrow)) {
transform.Rotate(0,turnSpeed * Time.deltaTime * turnSpeed,0);
}
var direction = transform.forward * Time.deltaTime * walkSpeed;
if (Input.GetKey(KeyCode.UpArrow)) {
transform.Translate(direction);
}
}
I've even tried removing the " Time.deltaTime turnSpeed" But it's just the same result. Any help would be appreciated.
Answer by Tahelu · Mar 30, 2012 at 05:48 PM
I am not sure whether i have correctly understood you question, but you can try this.
Replace the movement code as follows:
if (Input.GetKey(KeyCode.UpArrow)) { transform.position += transform.forward Time.deltaTime walkSpeed; }
Do let me know if it solved the issue.
Your answer
Follow this Question
Related Questions
4 Direction Movement Problem 1 Answer
Camera Orbit Changing Characters Direction, How? How To Stop It? 0 Answers
Rotate the same way no matter what direction facing? 1 Answer
Can't get speed of a rigidbody. 1 Answer
how to make my character walk automatically and when pressing a button change the direction 2 Answers