(NEED HELP!) Camera controlled by mouse Y, player controlled by mouse X
Hello everyone.
I am making a third person shooter game and I'm having some trouble creating the camera controls.
I want the camera to be controlled on the Y axis by the mouse moving up and down. I also want the player to rotate on the X axis when I'm moving the mouse Horizontally. Here is what I have done:
CameraMouseLook.js (attatched to the camera)
function Update ()
{
xRotation -= Input.GetAxis("Mouse Y") * lookSensetivity;
xRotation = Mathf.Clamp(xRotation, -60,60);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
And here is the PlayerMouseLook.js (attached to the player)
function Update ()
{
yRotation += Input.GetAxis("Mouse X") * lookSensetivity;
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
Should I have the camera as a child of the Player? That's what I'm doing now and then I can still rotate the camera along the X axis. If I don't have the camera as a child of the player then the camera is stuck in the starting position and will not follow the player.
I can upload some screenshots or GIFs if you want more details.
Answer by mikii123 · Jan 31, 2016 at 02:12 AM
Code seems good. The camera isn't following the player, because you need to assign the transform.position of it.
In what script? With what code? :P And I guess the camera not should be a child of the player prefab?
Unparent the camera object. $$anonymous$$ake it float separately. Than in the script Camera$$anonymous$$ouseLook inside Update add the line:
transform.position = PlayerTransform.position - PlayerTransform.forward * 3;
It returns an "unknown identifier 'PlayerTransform'" :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612062333im_/https://answers.unity.com/themes/thub/images/avi.jpg)