- Home /
Limiting a Mouse-follow rotation based on parent object rotation
I've been searching for a response for the last two days unsuccessfully, so I turn here. Here's the scenario:
I have a GameObject PHardpoint. I want it to be able to rotate to face the mouse, but only within a 35 degree angle off the centerline of a parent GameObject Ship (imagine a mounted gimbal weapon that can only twist so far). So far, using the code below, I have gotten it to Clamp properly only if the ship is facing towards the top of the screen (rotation at 0). If I start to rotate the ship in either direction, it clamps to the maximum rotation value and won't change until I'm back at 0.
void FixedUpdate()
{
diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg -90;
rot_z = Mathf.Clamp(rot_z, parentRotation.eulerAngles.z - maxGimbalAngle, parentRotation.eulerAngles.z + maxGimbalAngle);
angleToRotate = Quaternion.Euler(0, 0, rot_z);
transform.rotation = Quaternion.Slerp(transform.rotation, angleToRotate, Time.deltaTime * rotationSpeed);
}
You can see a gif of the issue here: http://i.imgur.com/eEYokWB.gifv The pink laser is what I'm trying to restrict.
Any help in this would be greatly appreciated.