How do a get a character to move in a direction relative to the cameras direction Without rotating him?
I have a setup in Mecanim where my player character can strafe forward, backward, left, and right, and all directions inbetween.
I need my character to be able to strafe (relative to the camera) no matter the direction he is facing, when both the cameras transform.forward and the characters transform.forward are constantly changing.
For example, if my character is facing the right, and camera facing forward. how do I convert what would be the Cameras positive Z (Up on the joystick) to move my character on his negative X (to his left & away from the camera)?
Also note that Mecanim is using the animations to actually move the character.
I know I need to put the two transform directions through an operation. but I honestly don’t know which.
I’m not sure if this is the information you are looking for… but here goes:
First you will need to compute the character’s position, relative to the camera’s position (this is a Vector3 subtraction operation). (note: if you want to make him move on say… a 2D floor, you will need to set the resultant “relative position”'s Z value to zero)
This relative position, can also be interpreted as a computed direction, in world space, and for your purposes, would equate to UP on the joystick. You can rotate this vector based on the joystick position (after computing an angle from the joystick position - you can use mathf.atan(x,y) for this), by using the Quaternion.AngleAxis() function. The axis you pass in to this function would be either, your Camera transform’s “up”, or (0,0,1), whichever suits your situation best.
Once you have the Quaternion rotation, just multiply it by the relative position vector, to get your final direction vector.