- Home /
camera acting weird...
hi everyone, i made an empty game object(called it "player") and inside of it i put my 3d model and the camera. i made a script and had it attached to the camera. what my script does is to move "player" on the y axis and my camera on the x axis. and i wanted to add a function that will make the camera move closer to the player when you look down. and so i wrote my function and it kind of work, the problem is that the camera dosent come closer only on the z axis like i want but it is also changing the x axis, and i have no idea why, please help me.
this is the code:
public float mouseSensitivity = 100f;
public Transform playerbody; ///empty game object player transform
float xRotation = 0f; /// camera rotation
public Vector3 firstLocation; /// first camera position
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
firstLocation = transform.position;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
playerbody.Rotate(Vector3.up * mouseX);/// player side movment
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -30f, 45f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); /// camera up and down movment
}
void LateUpdate()
{
LookDown(xRotation); /// function to move closer when you look down
}
void LookDown(float rotate)
{
float t = rotate / 45f;
if (rotate > 0)
{
transform.position = new Vector3(firstLocation.x, firstLocation.y, Mathf.Lerp(firstLocation.z, 0f, t));
}
}
}
Answer by cyudani · May 14, 2021 at 10:08 AM
i was able to fix the problem. the problem was that the camera is a child of "player" so i needed to use transform.localPosition and not position.