- Home /
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.
Answer by maddFrogg · Apr 29, 2014 at 10:28 AM
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);
Hm, it gives errors on forwardVel.x and forwardVel.z, that I'm using possibly unassigned fields.
Oddly enough, if I use the same type of code, but on assigning the horizontalVel, using forwardVel.x and z don't give any errors.
Yo need to initialize forwardVel vector, or swap forwardVel.x and forwardVel.z with numbers.
Initialize it in what way?, I've put it in the start() method, but I don't know what I'm meant to set it to, or what numbers to use in it's place, using 0 just makes the player float upwards and downwards in the air.
forwardVel = new Vector3(0, cameraTransform.transform.forward.y * speed * moveVertical, 0);
This is how you initialize a vector. If the variable is public you probably won't need to initialize it, as it assumes it's a Vector3.zero
I would need to see the code to know why it is floating around.
Hm, right:
public Camera theCamera;
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;
There it is, just to recap, I'm trying to get this set up so that the player moves relative to the camera, but won't go slower or faster by rotating the camera, which it does, in fact, if you look down on the character, trying to go backwards lets you fly in the camera's direction.
Though right now, for some reason I can't think of, the player just goes right up and down into the air, and changing the camera still lets you fly upwards.
Answer by pcbabu · Apr 29, 2014 at 12:29 PM
is should be cameraTransform.transform.right.x
not cameraTransform.transform.right
I don't think you know quite what's wrong, the line with .y is the broken one.
Your answer
Follow this Question
Related Questions
Can relative velocity be calculated from relative direction vector 3 Answers
Move JoyStick Together With Camera 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Calculate vector exactly between 2 vectors 4 Answers
Move Rigidbody Relative To Camera, quick Q (hopefuly) 1 Answer