- Home /
3rd Person movement in relation to camera
I've been trying for a while to try and understand how to move a character in relation to a camera's rotation. At the moment the movement that I have is that W is moving away from the camera, A and D are strafing to the left or right. Moving back simply just backs the character into the camera.
I've been following the 3D Buzz tutorial for the 3rd Person Controller System and it is a great tutorial, but the movement that I described above is something that I currently don't want. I want the character to move and rotate properly in relation to the camera.
the following code are the methods that handle the actual movement and rotation of the player:
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize Movevector if Magnitude is > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= Movespeed;
// Mulitply Movevector by DeltaTime
MoveVector *= Time.deltaTime;
// Move the Character in World Space
TP_Controller.CharacterController.Move(new Vector3(MoveVector.x, MoveVector.y, MoveVector.z));
}
void SnapAlignCharacterWithCamera()
{
if (MoveVector.x != 0 || MoveVector.z != 0)
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
I somewhat understand what everything does on a high level, but I am sadly not very well versed in things like rotation and Quaternions, and so because of this I am left in the dust because I have no idea how to use them. As far as I can understand, the method SnapAlign...() is used to tell the character that the direction that the camera is facing is forwards, but I don't know where to go from here?
So I guess my question is how do I actually move and rotate a character in relation to the camera, so that forward is moving away from the camera, backwards is running towards it and left and right are moving to the left or to the right of the camera.
[EDIT] I may have described my problem a little weirdly, sorry. When I move my character, the player is rotated so that it is facing forward in relation to the character, which causes the problems i am having.
Below is a crudely drawn diagram. (Sorry, I am truly no artist) The player faces forwards at all times, no matter which input is being applied (left, right, back etc.) I need to get get it so that the character rotates properly, e.g it will rotate left to move left.
Line 4 is what takes a local direction and transform it into a world direction. Right now you are using 'transform' which is the transform of the character to make this transformation. That means you are making your moves relative to the character. I believe you will get what you want if you use the transform of the camera to make that transformation:
$$anonymous$$oveVector = Camera.main.transform.TransformDirection($$anonymous$$oveVector);
I assume from the 'EDIT' that I did not understand your question. After reading your new text, I still don't understand the problem. A drawing might help as well as the relationship between the camera and the character.
I added a picture that might make things more clear. Sorry I'm really not very good at explaining things, especially things I don't understand myself
Answer by Professor Snake · Jul 19, 2013 at 08:21 PM
One solution would be to only rotate the character model based on the direction its moving, while keeping the character controller's rotation the same. Otherwise, you can use the camera's transform.forward and transform.right to get the direction it's facing, but it's not as elegant as the previous solution.
Your answer
Follow this Question
Related Questions
RTS Camera Rotation and Movement 0 Answers
How to make my player look in the direction of its movement? 3 Answers
camera slides and bounces while moving C# 1 Answer
Global movement 0 Answers
Move Camera around an object 2 Answers