- Home /
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));
}
Answer by senad · Aug 01, 2012 at 10:47 AM
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.
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?
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:
$$anonymous$$ath.abs( Input.GetAxis ("Horizontal") )
But in either case, the sign of your translation should be always the same. (positive or negative)
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.
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.
$$anonymous$$aybe I am understanding you wrong??
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.
:)
Your answer
Follow this Question
Related Questions
How do I translate my ship left and right and have it rotate at the same time? 1 Answer
Getting a 2d Sprite to move over time to an Array 1 Answer
How do I make an Enemy charge through a point? 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Make an object move to a given point by rotating to the correct direction first. 1 Answer