x


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.

more ▼

asked Mar 30, 2012 at 05:24 PM

Gantar180 gravatar image

Gantar180
13 9 6 8

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

1 answer: sort voted first

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.

more ▼

answered Mar 30, 2012 at 05:48 PM

Tahelu gravatar image

Tahelu
0

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

x6286
x2586
x516
x301
x63

asked: Mar 30, 2012 at 05:24 PM

Seen: 1276 times

Last Updated: Mar 30, 2012 at 05:48 PM