Question by
astroboyr · Jan 27, 2020 at 03:05 AM ·
rotationeuleranglesclampclamped rotation
MathF clamp look toward mouse pointer not working correctly
Hi, I'm trying to make a game where a cannon looks toward a mouse pointer. Here is my code
using UnityEngine;
public class CannonPoint : MonoBehaviour {
public GameObject cannon;
public float rotateSpeed = 5;
public float minAngle = 60;
public float maxAngle = -60;
void Update(){
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
if(Input.GetAxis("Mouse X") < 0) {
cannon.transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
}
if(Input.GetAxis("Mouse X") > 0) {
cannon.transform.Rotate(0, 0, -rotateSpeed * Time.deltaTime);
}
transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp (transform.eulerAngles.z, minAngle, maxAngle));
}
}
So, this works, but when I set the min angle to -60 and the max to 60, it doesn't. I want it to stop at -60 and 60. However, when the rotation goes to more than 0, it just sets to -60. Please help! Thanks!
Comment
Your answer
Follow this Question
Related Questions
How to Clamp a 3rd Person Camera's x-axis Rotation? 0 Answers
Object don't rotate correctly 1 Answer
Clamp a 2D object controlled by the mouse 0 Answers
Euler angles clamping issue 0 Answers