Quaternion LookRotation problems while moving up hill.

UPDATE: I have made a list of what I need help with. When I get one of these to work it will break the other. I need the things on this list to work together at the same time.

  1. I need the turret to stay attached to the tank properly while going up / down hill.
  2. I need the turret and gun to follow the camera’s raycast and line up accurately while going up / down hill.
  3. I need to be able to control the rotation speed of the turret.

Hello, I am having a few problems with a LookRotation that follows a camera’s raycast. I am controlling a tank’s turret and gun with a Raycast from the center of a camera, so the turret and gun look where the camera looks. When I am on flat ground the turret pivot and gun pivot look at the camera’s raycast correctly, but the first problem I encountered was driving up a angled surface. The turret and gun axis would break. I fixed this issue by adding a line of code which 0s out the euler angles for the rotation on the x and z axis (As I only care about Y on the turret). After doing that it messed up the accuracy of the Quaternion.LookRotation. Now when I drive up a hill the rotation of the turret pivot and gun pivot is not lining up which is causing the accuracy to be off. I have added my code and some screen shots of my problems. I need help to stop the turret from flipping off its axis while keeping the accuracy of the lookrotation tracking the camera’s movement when going up hill. Thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRaycast : MonoBehaviour
{
    public Transform TurretPivot;
    public Transform GunPivot;
    public float TurretRotationSpeed;
    public float GunRotationSpeed;
    private RaycastHit Hit;
 
	void Start ()
	{
	    TurretPivot = TurretPivot.GetComponent<Transform>();
	    GunPivot = GunPivot.GetComponent<Transform>();

	}
	
	void Update ()
    {
		TurretFollowCameraRay();
        GunFollowCameraRay();
	}

    void TurretFollowCameraRay()
    {

        if (Physics.Raycast(transform.position, transform.forward, out Hit))
        {
            Vector3 CameraRayDirection = Hit.point - TurretPivot.transform.position;
            Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
            TurretPivot.transform.rotation = Quaternion.RotateTowards(TurretPivot.transform.rotation, RotateToPosition, TurretRotationSpeed * Time.deltaTime);

            // This line of code fixes the turret's pivot axis, but breaks the accuracy of the LookRotation.
            TurretPivot.localEulerAngles = new Vector3(0,TurretPivot.localEulerAngles.y, 0);
        }
    }

    void GunFollowCameraRay()
    {
        if (Physics.Raycast(transform.position, transform.forward, out Hit))
        {
            Vector3 CameraRayDirection = Hit.point - GunPivot.transform.position;
            Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
            GunPivot.transform.rotation = Quaternion.RotateTowards(GunPivot.transform.rotation, RotateToPosition, GunRotationSpeed * Time.deltaTime);

            // This line of code fixes the gun's pivot axis, but breaks the accuracy of the LookRotation.
            GunPivot.localEulerAngles = new Vector3(GunPivot.localEulerAngles.x, 0, 0);
        }
    }
}

Your code really rely on raycast so there’s chance that your raycast might not hit anything when aiming up,

you can try add invisible collider on the roof or pass a hit point if the raycast did not hit anything

TurretPivot.transform.rotation redundand code. You already declared TurretPivot as Transform. just use TurretPivot.rotation instead. Otherwise it’s like this.gameObject.transform.transform.rotation

Now to the problem:

just use:

//specify rotation, where second argument is what should be the "upwards dir of the object"
Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection, TurretPivot.up);

if you are using turret.FBX, try .right instead


for Gun use GunPivot.right, because it rotates around its local right axis

And then dispose of the y-angles code and the x-angles code you were using.