Move relative to camera but not vertical

Hey guys,

I have my player which is an empty game object with a capsule collider and rigidbody. The player has a child which is the camera. I have a script attached to the camera that is essentially for a mouse look. I have a playercontroller script attached to the player that I want to use to control the player’s movement. I’m struggling with getting the player to move towards the “forward” direction the camera is facing but only on the horizontal axis (I the player is looking up or down with the mouse I don’t want the player to start flying up or down).

Any help would be greatly appreciated. I’ve been googling solutions for hours but each one I test has yet to work out. I must be missing something.

I’m personally not familiar with the playercontroller script but if you want to get the forward direction of the camera try:

Camera.main.transform.forward
(You could also just store the camera in a Transform variable and use: variableName.forward)

Remember:
Transform.forward is local space.
Vector3.forward is global space.

Me either I am not using the playercontroller script I was making my own. It did not cross my mind to use a Transform variable I will look into it.

Second question if you don’t mind: Is it possible to get the Input.GetAxis(“Horizontal”) of a different game object that the script isn’t attached to? I tried something like:
public GameObject cam;
float h = cam.Input.GetAxis(“Horizontal”);

But that obviously threw all kinds of errors haha I’m essentially wondering if my player object/class can get that horizontal axis imformation from the camera in the scene (which is a child of the player)?

Input.GetAxis (“Horizontal”) actually only returns a float value between -1 and 1 depending on the buttons your pressing (a and d). The idea would be to multiply a direction by the input data and then multiply that by another float to change the speed.

Try something like:
myRigidbody.MovePosition(transform.position + camera.transform.forward * Input.GetAxis (“Horizontal”) * speed * Time.deltaTime); //Time.deltaTime will make it move at the same speed regardless of your framerate

I’m still a little confused about your goal here, but you might want to use the “Vertical” axis instead.

Yes Vertical is what I meant haha

All of this wouldn’t be an issue if I could just control the camera’s movement as the player. But I had to create an empty object with the capsule collider and make the camera a parent of it. I did this because when I had the camera as the only object for the player with a collider the collider would rotate with the mouse look and I can’t have that happen. Sorry if that is confusing. Nonetheless, thank you for your suggestion it gives me something to play with.

Just zero out the Y component of the camera’s transform forward.

Vector3 direction = Camera.main.transform.forward;
direction.y = 0;

transform.position += direction * speed * Time.deltaTime;
1 Like

Yes that’s it! Didn’t realize I could zero it out that way. Thank you so much for your help dude :slight_smile:

1 Like

Almost 5 years later and that was the answer to my problem too! Thanks! Was searching for hours.