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.
- I need the turret to stay attached to the tank properly while going up / down hill.
- I need the turret and gun to follow the camera’s raycast and line up accurately while going up / down hill.
- 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);
}
}
}