# Turrets acting oddly when clamped

 0 I've got a setup where I have a block that spawns 4 turrets - 2 on top, and 2 on the bottom. I wanted to clamp them so that they could only rotate 180 degrees laterally (90 left, 90 right) and 90 degrees vertically (from straight ahead to vertically up, relative to their position)Clamping them horizontally worked okay. However, when clamping them vertically, whenever the turrets target something out of their movement range (i.e. less than 0 degrees, more than 90) they switch rapidly between their clamped values, so that they're bouncing up and down. How on earth do I get them to behave like they do horizontally?Note on the turret setup - they have two bones for movement. One is the base (horizontal) and one is the barrel (vertical) each with their own relatively simple script. I'll paste the barrel one here, as that's the one with the issue. The base script is the same, except it's all on the y axis. ``````var Target:Transform = null; var SwivelSpeed = 32.0; var localRotation : Vector3 ; //these 3 variables are here so I could check var clampedRotation : float ; //the values in the inspector var targetAngle : float ; function Update () { if(Target) { var localTarget: Vector3 = transform.InverseTransformDirection(Target.transform.position - transform.position); targetAngle = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg; var targetAngle2 = targetAngle - 90; var adjustedAngle = Mathf.Lerp(0, targetAngle2, Time.deltaTime * SwivelSpeed); transform.Rotate(Vector3.right, adjustedAngle ); //rotate the turret localRotation = transform.localEulerAngles ; //get the local rotation values clampedRotation = Mathf.Clamp(localRotation.x, 0, 90); //clamp to the values we want //force the turret rotation to be clamped transform.localEulerAngles.x = clampedRotation; transform.localEulerAngles.y = 0 ; transform.localEulerAngles.z = 0 ; } ``````Also, the fire control for the turret comes from another script in the hierarchy. How can I set it so that a turret that is unable to aim at the target can't fire? (I could use a raycast on each turret, but I'm after the cheapest method possible - I intend to have lots of turrets. more ▼ asked Jul 15 '12 at 08:52 AM PiMuRho 18 ● 2 ● 4 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

1 answer: sort voted first
 0 Okay, here's how I fixed it (both issues in one) ``````if(Target) { var localTarget: Vector3 = transform.InverseTransformDirection(Target.transform.position - transform.position); var targetAngle : float = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg; var targetAngle2 : float = targetAngle - 90; var adjustedAngle = Mathf.LerpAngle(0, targetAngle2, Time.deltaTime * SwivelSpeed); var localRotation : Vector3 = transform.localEulerAngles ; var expectedNewAngle : float = transform.localEulerAngles.x + adjustedAngle; if (expectedNewAngle > 1 && expectedNewAngle < 90) { transform.Rotate(Vector3.right, adjustedAngle ); allowFire() ; } if (expectedNewAngle < 1 || expectedNewAngle > 90) { ceaseFire(); } } } ``````ceaseFire() just calls a function on the projectile generator and stops it from firing. So now my turrets have nicely clamped movement, and they don't fire when they can't "see" the player. more ▼ answered Jul 18 '12 at 02:30 PM PiMuRho 18 ● 2 ● 4 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x724
x172
x81

asked: Jul 15 '12 at 08:52 AM

Seen: 275 times

Last Updated: Jul 18 '12 at 02:30 PM