I’ve got the following code handling the rotation of a child object on the game’s player object.
private void Update()
{
input = Mouse.current.delta.ReadValue();
}
private void LateUpdate()
{
if (axes == RotationAxes.MouseXAndY)
{
vertical += input.y * sensitivityY;
vertical = Mathf.Clamp(vertical, minimumY, maximumY);
horizontal += input.x * sensitivityX;
horizontal = Clamp(horizontal, minimumX, maximumX);
if (rotateObject == null)
transform.rotation = Quaternion.Euler(vertical, horizontal, 0.0f);
else
{
//Normally vertical would be applied to the X axis, but because of the bone's orientation, we apply it to the Z axis.
rotateObject.transform.rotation = Quaternion.Euler(0.0f, horizontal, vertical);
}
}
else if (axes == RotationAxes.MouseX)
{
horizontal += input.x * sensitivityX;
horizontal = Mathf.Clamp(horizontal, minimumX, maximumX);
if (rotateObject == null)
transform.rotation = Quaternion.Euler(0.0f, horizontal, 0.0f);
else
rotateObject.transform.rotation = Quaternion.Euler(0.0f, horizontal, 0.0f);
}
else
{
vertical += input.y * sensitivityY;
vertical = Mathf.Clamp(vertical, minimumY, maximumY);
if (rotateObject == null)
transform.rotation = Quaternion.Euler(vertical, 0.0f, 0.0f);
else
{
//Normally vertical would be applied to the X axis, but because of the bone's orientation, we apply it to the Z axis.
rotateObject.transform.rotation = Quaternion.Euler(0.0f, 0.0f, vertical);
}
}
}
private float Clamp(float value, float min, float max)
{
if (value < min || value > max)
return 0.0f;
else
return value;
}
The Clamp method above is because the Mathf.Clamp method doesn’t allow the rotation to go back to 0 once a full circle is made.
Now the problem I’m having is that when the player presses the appropriate input to begin rotating this object, it automatically turns it to a weird orientation. I’m assuming this is because the default values of “vertical” and “horizontal” are 0.0f to start. Question is, how can I get the appropriate rotation values from the “rotateObject” in order to set “vertical” and “horizontal” to the correct default values (which would be whatever the values of the rotateObject’s rotation is when this script is enabled.