Hello, I’m trying to have a player hold an object in front of him in first person, however, when I look up and down, the object appears to be getting farther apart than when looking straight forward, which does not look correct. Here is a video: wvu7wk and my hierarchy: Imgur: The magic of the Internet. If I am not mistaken, my cube should follow the main camera and rotate with it, so it should stay a fixed distance in front of it. Here is my player look script, which is attached to the camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour {
float INPUT_MULTIPLIER = 5.0f;
float xRotation = 0.0f;
public Transform playerTransform;
void Start() {
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
float mouseX = Input.GetAxis("Mouse X") * INPUT_MULTIPLIER;
float mouseY = Input.GetAxis("Mouse Y") * INPUT_MULTIPLIER;
float newXRotation = xRotation - mouseY;
xRotation = Mathf.Clamp(newXRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerTransform.Rotate(Vector3.up * mouseX);
}
}
I cannot seem to get it working. Any help is greatly appreciated.