- Home /
Camera Relative Movement
I'm trying to write my own controller for a third person character and I'm having some trouble making the character move relative to a camera i.e. pressing up will always make the character walk away from the camera, pressing right will always cause the character to move right relative to the camera.
Here's my current code for setting the rotation of the character:
private void updateRotation(){
Transform cameraTransform = theCamera.transform;
// forward of the camera on the x-z plane
Vector3 cameraForward = theCamera.transform.TransformDirection(Vector3.forward);
cameraForward.y = 0f;
cameraForward = cameraForward.normalized;
Vector3 cameraRight = new Vector3(cameraForward.z, 0.0f, -cameraForward.x);
float vert = Input.GetAxisRaw("Vertical");
float hor = Input.GetAxisRaw("Horizontal");
Vector3 tempTarget = hor * cameraRight + vert * cameraForward;
if (tempTarget!= Vector3.zero){
theController.moveDirection = Vector3.RotateTowards(theController.moveDirection, tempTarget, theController.rotSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
theController.moveDirection = theController.moveDirection.normalized;
theController.transform.rotation = Quaternion.LookRotation(theController.moveDirection);
}
}
And here is how my controller moves the character forward:
private void moveCharacter(){
transform.Translate(transform.forward*moveSpeed*Time.deltaTime);
}
Currently the character moves relative to the camera at first, then seems to "lose it's axis" so to speak. Any help would be appreciated.
Answer by robertbu · Jul 21, 2013 at 04:06 PM
I believe the root of the issue is that Transform.Translate take a Local not a World coordinate. The character movement code should be:
transform.Translate(Vector3.forward*moveSpeed*Time.deltaTime);
or you could do:
transform.position += transform.forward*moveSpeed*Time.deltaTime;
A few other comments:
Lines 4-6 can be replaced by 'theCamera.transform.forward' (which is normalized).
Your cameraRight code does not calculate the right of the camera...it is an angle right. If you want the actual right (which I think you do) you can just use 'theCamera.transform.right'.
Why GetAxisRaw() instead of GetAxis()?
So you can reduce lines 1 - 12 to:
Vector3 tempTarget = Input.GetAxis("Vertical") * theCamera.transform.forward + Input.GetAxis("Horizontal) * theCamera.transform.right;
I thought the problem might have been something not being in world space, but I couldn't figure out what. I was mainly using code from a tutorial to implement this, hence the GetAxisRaw() and the unnecessary code. Thanks for your solution, it worked perfectly! Edit: The reduction you suggested results in a vector with a non-zero y-axis, it needs to be zeroed first i.e.
Vector3 tempTarget = Input.GetAxis("Vertical")* theCamera.transform.forward + Input.GetAxis("Horizontal") * theCamera.transform.right;
tempTarget.y = 0f;
This way the character only rotates around the y-axis i.e. on an x-z plane
Answer by meat5000 · May 26, 2014 at 05:27 PM
I know its old but I wrote one recently so I'll write some tips while its fresh in my mind.
I wrote for Android, so a visual stick is present on screen.
I made animations for Walk/Run forward, backward and turn left, turn right.
Stick.Y = Walk
Stick.X = Turn.
So normally, up on stick makes character walk forward no matter his facing direction. This is your link between Camera and Joystick. Up on stick and Forward on Camera correspond to 0 degrees.
The formula goes
Jr = -P + Jp
(Jr = Joystick Rotation Required, P = Player rotation from camera, Jp = Joystick Press Rotation)
So, find clockwise rotation of Player (P) with respect to camera forward. Make it Negative. Use AngleAxis to create a rotation of this angle around Vector3.up. Multiply the Quaternion by your input stick vector.
Note, mapping Stick x,y to Vector3 x,z helps with simplistic Vector2 rotation. Also one will require the use of Vector3.cross to find Angles over 180 degrees.
Your answer
Follow this Question
Related Questions
Relative Movement Problem 1 Answer
Global movement 0 Answers
Calculate vector exactly between 2 vectors 4 Answers
Problem with camera position 3 Answers
In-Air movement relative to camera 1 Answer