Applying a rotation relative to the object's starting rotation
I am trying to simulate turret-like behavior, anchoring an object to a vehicle using parenting. So I have made this script for the turret rotating with my mouse (moving the mouse vertically changes the elevation and horizontal angle of the turret). This script works fine for turrets which lie on the X-Z plane, but I want it to be able to position the turrets at different angles.
void Start()
{
startingRotation = transform.eulerAngles;
if (useAzimuth)
{
minAzimuth = -azimuth / 2f;
maxAzimuth = azimuth / 2f;
}
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation += mouseY;
xRotation = Mathf.Clamp(xRotation, minElevation, maxElevation);
yRotation += mouseX;
if (useAzimuth)
{
yRotation = Mathf.Clamp(yRotation, minAzimuth, maxAzimuth);
}
transform.localEulerAngles = new Vector3(xRotation, yRotation, 0f) + startingRotation;
}
Comment