Question by
DohnnyJepp · Jan 06, 2018 at 04:22 AM ·
rotationmousepositionlimited
Limited rotation towards mouse
Hello, I have 2D game, 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 now works fine, but gun doesn't look towards mouse.
void Update() { lockedRotation(); } void lockedRotation() { 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!
subimg.jpg
(45.4 kB)
Comment
Your answer