Limiting how high or low a cannon can aim
I'm trying to make a tank barrel aim up and down but I'm not sure how to get the barrel to stop when it reaches a certain point. I'd rather not use colliders to do this.
Thanks
Answer by PoptartCrank · Jul 12, 2017 at 11:28 AM
void Update () { var ud = Input.GetAxis("TurretUD") Time.deltaTime m_BarrelSpeed; transform.Rotate(0, ud, 0);
Currently using this to rotate it
Answer by Merhex · Jul 12, 2017 at 11:49 AM
You can use Mathf.Clamp() to clamp the value between 2 points ?
Answer by OhnoNono · Jul 12, 2017 at 12:45 PM
I'm having the same problem and use the same method to rotate my cannon.
I have tried with Mathf.Clamp and my cannon goes crazy.
public float turnSpeed = 1f;
public float maxAngle = 100f;
public float minAngle = 89f;
private float currentRotation;
Transform myT;
// Use this for initialization
void Start ()
{
myT = transform;
}
// Update is called once per frame
void Update ()
{
currentRotation = Input.GetAxis("Cannon") * turnSpeed * Time.deltaTime;
//currentRotation = Mathf.Clamp(currentRotation, minAngle, maxAngle);
myT.Rotate(-currentRotation, 0, 0);
}
My cannon spins like crazy and the the rotation is not restricted.
x = *
You need to assign it back rotate =$$anonymous$$athf.Clamp(rotate , $$anonymous$$Rotate, maxRotate);
That is what is tried and i can't get to work.
Forgot to type some of my code :)