x


Vector-based movement?

I have a script that handles rotation and movement for a character rigidbody. It rotates properly, and moves correctly in one direction. I'm using Input.GetAxis("Horizontal") to determine if the character should move left or right, but when applied to a Translation, the character ALWAYS moves in the positive direction, even when the input is negative. Any ideas?

void FixedUpdate(){
// LEFT/RIGHT ROTATION
Vector3 dir = new Vector3(transform.position.x + (Input.GetAxis("Horizontal") * 5), transform.position.y, transform.position.z);
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(0, 0, Input.GetAxis ("Horizontal")));
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10.0f);

float translation = Input.GetAxis ("Horizontal") * moveSpeed * Time.deltaTime;
Debug.Log (translation);
transform.Translate (new Vector3(translation, 0, 0));

}

more ▼

asked Aug 01 '12 at 08:15 AM

Razion gravatar image

Razion
118 2 8 11

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

1 answer: sort voted first

You use the same input values for rotation and movement at the same time. Is that intentional?

Might it be that your game object gets rotated in such a way that the movement will always happen in only one direction? Try commenting out the rotation code and see what happens to the translation.

more ▼

answered Aug 01 '12 at 10:47 AM

senad gravatar image

senad
641 3 5

Yeah, that was the problem. Both of those things are dependent on the same input axis though. Any ideas how I could go about getting them to play nicely with each other?

Aug 01 '12 at 11:26 AM Razion

It depends on what you want to do. So what do you want to do??

If you want your moveSpeed to be constant you can do something like this:


float translation = Vector3.forward * moveSpeed * Time.deltaTime;

If you want it to stay dependent on the input axis range, you can just take the absolute of that value. Something alont the lines of:


Math.abs( Input.GetAxis ("Horizontal") )

But in either case, the sign of your translation should be always the same. (positive or negative)

Aug 01 '12 at 11:38 AM senad

That's the problem, though. Using an absolute value for either translation or rotation will only allow movement/rotation in one direction.

When horizontal = 1, I want to rotate to face right (which works), and when it's -1, I want to rotate to face left (which also works).

When I attempt to add translation (negative for left, positive for right), I always move to the right because my rotation is throwing me off. (This is a sidescroller)

Using Vector3.forward isn't an option as far as I know, because my rotation takes .5 seconds to go from 0 to 1 or -1 for the sake of animation purposes.

Aug 01 '12 at 07:45 PM Razion

Yes, so as I understand it, your rotation and translations add up and let your char move in the same direction each time.

So what I think you should do is always use positive translation values and leave the rotation as it is.

Maybe I am understanding you wrong??

Aug 02 '12 at 08:37 AM senad

I see. :)

If it is a side scroller, all you want to do is move your char left or right in world space. There is no need to use local model space.

Just add the parameter "Space.World" to your transform.Translate() call. Then the rotation will have no effect on your translation.

:)

Aug 02 '12 at 09:24 AM senad
(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:

x1363
x195
x41

asked: Aug 01 '12 at 08:15 AM

Seen: 463 times

Last Updated: Aug 02 '12 at 08:03 PM