- Home /
Moving relative to camera direction at constant speed (Velocity problem)
Alright, so I want to move a gameObject (Player) relative to the direction that its camera is facing without affecting height (and gravity).
This is my actual code that returns as a function a Vector3 that indicates the velocity to move the player relative to the forward of the camera.
private Vector3 GetBaseInput()
{
//Return Vector3 of 0 if no key pressed.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{
p_Velocity += cam.transform.forward;
}
if (Input.GetKey(KeyCode.S))
{
p_Velocity += -cam.transform.forward;
}
if (Input.GetKey(KeyCode.A))
{
p_Velocity += -cam.transform.right;
}
if (Input.GetKey(KeyCode.D))
{
p_Velocity += cam.transform.right;
}
p_Velocity.Set(p_Velocity.x, rb.velocity.y, p_Velocity.z);
Vector3.
return p_Velocity;
}
However, although this doesn't affect Y position of the player at all, the velocity given by W and S keys is slow if I walk while watching to the ground or to the sky.
I want to move at a constant speed to the direction that the player is facing, without affecting its Y position. How can I achieve this?
PS: Sorry for my bad english or explanation, I don't speak english natively.
You speak portuguese? If so, we will speak in Portuguese to better understand what your mistake is.
Your answer
Follow this Question
Related Questions
Touch GUITexture and Game object moves left or right, how can i do that? 0 Answers
Character Controller won't move forward 0 Answers
movement doesnt stop when jump pad is tapped at the same time 0 Answers
Unity movement forward doesn't work as expected 1 Answer
Character rotates left and right instead of going forward. 1 Answer