- Home /
How to make player move in the direction of where the camera is facing.(Y axis only)
I have a First Person game, where if you move the mouse, the camera rotates. I also have a script for when you use the WASD keys, the player moves accordingly. But when the camera rotates, the player still moves in the same direction. This is logical, but how can I make the player's movement's direction change with the camera's rotation on the Y axis. If you need any help understanding what's going on, just ask.
Please either accept the answer that has resolved your concern, let us know it has not been resolved, or close the question.
Answer by clunk47 · Jul 22, 2013 at 08:47 PM
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);
This will keep the player's local Y angle with the main camera as it rotates. Make sure the camera is tagged "MainCamera". Having only rotate the player itself on the Y axis will keep it from rotating sideways / back and forth if the camera looks around in different directions. Say your camera is high up, and moves lower, but still has to look at the player. This requires the camera to change angles on the x axis, which you don't want your player to do.
everytime my player rotates with camera , the camera shakes . Why is that?
This worked for me my player now rotates when i move my mouse left and right and the camera stays behind him ins$$anonymous$$d of rotating in a 360 degree movement around my player which I wanted but now I want the player to move in the direction of of the rotation and what is currently happening is I press W to walk forward he walks forward but when I rotate my mouse left or right the player will continue to walk forward in that same direction and not move forward in the direction I rotate any ideas on how can i fix this?
Answer by rhys_vdw · Jul 25, 2013 at 03:16 AM
Although clunk47's answer is correct, I would suggest a different approach.
Child the camera to the player, and change your camera movement script to rotate the player when the mouse moves horizontally, and rotate the camera when the mouse moves vertically.
If that is not clear, post your code and I'll give you some tips.
Hi sir im a beginner and im having the same trouble. I've parented the main camera already, what's next?
This is the code for player's movement:
void FixedUpdate()
{
//Store input axes
float lh = CrossPlatformInput$$anonymous$$anager.GetAxisRaw("Horizontal");
float lv = CrossPlatformInput$$anonymous$$anager.GetAxisRaw("Vertical");
$$anonymous$$ove(lh, lv);
Animating(lh, lv);
}
void $$anonymous$$ove(float lh, float lv)
{
//$$anonymous$$ove the player
movement.Set(lh, 0f, lv);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.$$anonymous$$ovePosition(transform.position + movement);
if (lh != 0f || lv != 0f)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), turnSpeed * turnSmoothing * Time.deltaTime);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
targetRotation = transform.rotation;
}
}
Answer by roojerry · Jul 22, 2013 at 07:05 PM
use camera.transform.forward
Um, I don't think you understand. I want the camera to o all the rotating, and the player to move. You see, if I press W, my player goes forward, and when my camera rotates, he still goes forward in the same direction he was going. I want him to go in the direction of the camera.
so, move your player in the forward direction of the camera
What code are you using to move your player forward at the moment?
Answer by MalachiteBR · Jul 23, 2013 at 11:56 AM
As the previous answer stated, you need a script that do something like that:
Update()
{
transform.forward = GameObject.Find("MainCamera").forward;
}
This way your character will have its "forward" always pointing to camera forward.
If you like my answer, upvote it plz :)
Would be GameObject.Find("$$anonymous$$ainCamera").transform.forward if you want to use the transform of the camera.
You could also just use Camera.main.transform if you're referencing the $$anonymous$$ain Camera.
But if you make the player's transform.forward the same as the camera's forward, the player object will rotate on ALL the axes as the camera does. He wants Y axis only.
That line wont work, as Find returns a GameObject. You'll need to get its transform. A more succinct way of writing this would be:
transform.forward = Camera.main.transform.forward;
Answer by woody3d · Jul 29, 2014 at 01:18 AM
No one will ever give you the simple answer around here. Too busy trying to look big here of all places. LOL. Just make sure you have NO mono behavior scripts on you CAMERA in Inspector, Instead, go to the FirstPersonController Prefab (I assume this is what you are using) and add the component MouseLook Script under camera control. I think you ere adding the script to the camera which was sending your camera looking in a different direction without telling your 'controller' whats going on... Applying it to the top of your firstpersoncontroler hierarchy lets the whole chain benefit from the Mono behavior script.
I'm only 2 lessons into learning Unity but that worked for me! essentially, Make sure you have the behavior only on the controller not on the camera if it's linked!
Thanks for replying an answer, but take a look at the year this was posted haha. I'm a bit beyond this level of Unity.
Your answer
Follow this Question
Related Questions
Player jitters when moving. 1 Answer
Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers
Rotation of player based on camera direction and joystick direction in 3D world 1 Answer
Camera Rotating when the player reach a specific position 0 Answers