In games like Warzone/Apex/Battlefield when you stand on the roof of a moving vehicle such as a car or any rotating object, you will follow the movement and rotation with no issues rather than just standing still and falling down once the car moves a certain distance. My main issue is the rotation part if a vehicle or object is rotating. I parent my player (probably not the best) but I fix its scale so it doesn’t change and I believe I solved another few edge cases so the movement portion when parented works well.
I know it’s not there by default with a controller but I have tried several methods and either get gimbal lock or the player rotates on its own separately to the rotating object being stood on. For example if I step onto the roof of the car the camera will snap to match its rotation I believe, then stepping off does the same thing. My latest camera script seems to have fixed the snapping, but the player rotates on its own on top of rotating alongside the car when it spins around etc.
Are both the issue and solution tied to having a proper camera/mouseLook script? My mouseLook script that I made is attached to my player and I keep my camera so that it’s not a child of the player since I do things like freelook, etc, which makes it easier like this.
public class MouseLook : MonoBehaviour
{
public float mouseSensX = 2f;
public float mouseSensY = 2f;
public Transform headBone;
public Vector3 cameraOffset = new Vector3(0f, 0.1f, 0f);
private Transform cam;
private Quaternion targetRotation;
private float rotationX = 0f;
private float rotationY = 0f;
void Start()
{
cam = Camera.main.transform;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensY;
rotationY += mouseX;
targetRotation = Quaternion.AngleAxis(rotationY, Vector3.up);
rotationX -= mouseY;
rotationX = Mathf.Clamp(rotationX, -85f, 85f);
Quaternion cameraRotation = Quaternion.AngleAxis(rotationX, Vector3.right);
transform.rotation = targetRotation;
cam.position = headBone.position + cameraOffset; // headBone is the head bone/obj of my rigged humanoid model
cam.rotation = targetRotation * cameraRotation;
}
}