- Home /
Question by
chucky831 · Mar 06, 2020 at 11:12 AM ·
scripting problemrotatemathtrigonometryclamped rotation
clamp rotation by mouse direction
I have a problem with the clamp. I have to rate it so that my gun follows the direction of the mouse but with a rotation limit. When the player is turned to the right it works well but when it is turned to the left from problems. Once the weapon reaches -90 ° it fires directly at the rotation of 90 °. How can I solve it?
mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
posScreen = Camera.main.WorldToViewportPoint(gunPivot.position);
mousePos.z = 0;
var dir = (mousePos - posScreen).normalized;
angle = (Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
if (!check) //if player is turn to left
angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg + 90f;
angle = Mathf.Clamp(angle, -90f, 90f);
gunPivot.transform.localRotation = Quaternion.Euler(0, 0, angle);
Comment
Your answer
