I have 2D game (unity, c# scripts), where is a submarine with tower (parent) and gun (child).
- Tower can rotate (when I press A or D) around submarine body. (works fine)
- Problem is the gun on tower should rotate towards mouse, but only in angle limitation. Angle limitation (by parent) now works fine, but gun doesn't look towards mouse.
void Update() {
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(mousePosition.x -
transform.localPosition.x,mousePosition.y - transform.localPosition.y);
float angle = (Mathf.Atan2 (direction.y , direction.x) * Mathf.Rad2Deg);
transform.localRotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Min(
Mathf.Max( Mathf.Abs(angle),40 ),140 )));
}
Here is a video where you can see how it "works" now: https://youtu.be/1pm54cjzYxA
Thanks for help!