- Home /
Turrets acting oddly when clamped
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.
Answer by PiMuRho · Jul 18, 2012 at 02:30 PM
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.
Your answer
Follow this Question
Related Questions
Rotate Turret where touched (top down view) 1 Answer
How to make the turret on your tank turn 0 Answers
Mini Gun Turret 1 Answer
Clamping a wrapping rotation. 6 Answers
Turret rotation clamp. 2 Answers