Character movement relative to camera doesn't work the way I intended.
Hey,
I'm working on a third person character controller with a camera that orbits the character. The camera can be moved around as well as above the character. After lots of trial and error I figured out how to get movement relative to the current position of the camera, however, my code has an unintended side-effect. When I move the camera above the character then "forward" is directed towards the ground, which makes sense but is entirely unproductive. Is there a way to solve this? I feel like a good way would be by accessing the rotation of the pivotpoint which my camera is attached to. How can I do that?
Here's the code I'm using
float UpdateMovement()
{
// Movement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 inputVec = new Vector3(x, 0, z);
inputVec = Camera.main.transform.TransformDirection (inputVec);
inputVec *= runSpeed;
controller.Move((inputVec + Vector3.up * -gravity + new Vector3(0, verticalVel, 0)) * Time.deltaTime);
// Rotation
if (inputVec != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(inputVec), Time.deltaTime * rotationDamping);
return inputVec.magnitude;
}
Come on, guys. It can't be that complicated. I just need a pointer...
Answer by Schecko · May 25, 2016 at 07:16 PM
Okay, the answer was really simple. I changed
inputVec = Camera.main.transform.TransformDirection (inputVec);
to
inputVec = target.transform.TransformDirection (inputVec);
and made my gameobject "pivotPoint" the target