Relative player controls velocity to camera rotation.

Hello, I’ve run into an issue, and I don’t understand it at all, see, I’m trying to get the player to move relatively to the camera, which is an orbiting camera.

However, I need to it to ignore the height of the camera, and only use the general direction, however:

public Transform cameraTransform;
public float speed;
 
public Vector3 forwardVel;
public Vector3 horizontalVel;
 
void Update()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
 
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
forwardVel = new Vector3(forwardVel.x, cameraTransform.transform.forward.y * speed * moveVertical, forwardVel.z);
horizontalVel = new Vector3 (horizontalVel.x, cameraTransform.transform.right.y * speed * moveHorizontal, horizontalVel.z);
 
rigidbody.velocity = forwardVel + horizontalVel;
}

This code results in a bizzare bug, the player moves perfectly normally, if I just keep teh camera where it is or at a 180 degree turn, basically, turning the camera to face downwards on top of the player, or putting the camera at the right or left, these both make the character (which is a rigidbody) just slow down almost to a halt, I don’t know what’s causing this or how to fix it.

You cannot assign a single Vector3 component, but the whole Vector at once.

Your code should look like this

Vector3 forwardVel = new Vector3 (forwardVel.x, cameraTransform.transform.forward.y * speed * moveVertical, forwardVel.z);

is should be cameraTransform.transform.right.x not cameraTransform.transform.right