Movement Problem

Hi all!

I have a movement script that the player uses their arrow/wasd keys to move the player around. The problem is that my back arrow key and the s key move the player forward instead of backwards. Here’s my code:

float vertical = Input.GetAxis("Vertical");

transform.position += transform.forward * 1 * vertical * Time.deltaTime;

I’ve been working on this for a couple days and can’t figure it out. Any help would be appreciated!

Is your camera pointing forward? I don’t see anything wrong with that code. If your camera is pointing the right way, then it might be your input settings for the Vertical axis. Try adding Debug.Log(vertical); and see if it is printing -1 when you press s.

It does print -1. When printing

transform.forward * 1 * vertical * Time.deltaTime

it gives (0.0, 0.0, -0.1).

Is this a first person game? If so, then this tells me that your camera is pointing backwards, so you are moving “back”, but since you are looking backwards, that makes it look like you are moving “forward”. Try making sure your camera’s rotation is zero.

When I do make my camera’s rotation 0, the player only moves backwards.

Is there more to your movement code than this, like a rotation section?

Here is everything:

public Animator Anim;
float speed;

void Update()
{

float vertical = Input.GetAxis(“Vertical”);
float jump = Input.GetAxis(“Jump”);
float horizontal = Input.GetAxis(“Horizontal”);

//Debug.Log(vertical);

bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately(vertical, 0f);
bool hasJumpInput = !Mathf.Approximately(jump, 0f);

transform.position += transform.forward * 1 * vertical * Time.deltaTime;
//Debug.Log(transform.forward * 1 * vertical * Time.deltaTime);

transform.Rotate(0, Input.GetAxis(“Horizontal”) * 2, 0);
Anim.SetBool(“IsMoving”, hasVerticalInput);

}

When you say that the player only moves backwards, does that include any button you press? Both w and s? And is the player moving in space, or just animating?

When I move the player, I use my arrow and ws keys. The player is both animating and moving.

And it moves backwards when you press w? If it is still printing -1 when you press w, then that looks like the axis needs to be adjusted, since it should print 1. Make sure that w is a positive button in the input settings (Project Settings → Input) and s is a negative button.

W does print 1 and S does print -1.

Could it possibly be my animations?

Could be, if they are overwriting the transform’s position.

I’m not quite sure what you mean by ‘overwriting the transform’s position’. Could you explain please?

Animations in Unity can control most things: variables in scripts, the name of objects, and values in a transform. If one of your animations directly animates your player object’s transform, then it will overwrite (replace, take priority over, ignore) the values your scripts try to assign to the transform. You could try disabling the animator temporarily to see if that’s the problem. If it is, then you’ll need to track down the animations that are controlling the transform’s position and make them only affect the player’s model, not the player’s base object.
Ideally, your player object should be structured something like this:
Player (base object) – No no model, movement script is attached
— Model (child of Player) – Animator attached
— Physics (child of Player) – Colliders if you want something more complicated than a single collider, like in a shooter for example

Then the movement script moves the base object, and the animations only affect the child object.

If I uncheck ‘Apply Root Motion’ in the animator component, then it moves in the desired direction, but when it stops, it jumps forward and then jumps back to the former position.

My player object is set up in such a manner. It does seem to be my animations. Any tips on how to fix this?

I’d start with an empty animation controller and add animations one at a time to find which ones are causing problems. Debugging animations problems is difficult without having the controller and animations in front of me. When you find an animation that is breaking your movement code, try examining that animation in the Animation window and see if it has keyframes for the base player object’s transform. If you find any of those, remove them and adjust the animation if needed.
Sometimes animators can still override the values of a object even if the animator no longer references that object. If you think that is happening, you can copy all of the states and transition in your animation controller and paste them into a new one. The new controller won’t have any of the dead references, so you can use the new animation controller instead of the old one in your player’s animator.