Turret Angle Limiting

Good afternoon good people of Unity3D!

Today I have a small issue that i’ve been meaning to fix for a while - my partner keeps telling me that the effects of my programming look half-assed :stuck_out_tongue: So I ask of you good folk, how can I limit the angles at which a turret can aim? At the moment I’m using a script similar to that in the tornadotwins’ tutorials to control a turret which has a texture (an arm and gun) attached to it - when the saucer is overhead this looks slightly strange because this poor man’s shoulders are protruding from his stomach. Here is the original script;

var LookAtTarget : Transform;
var damp = 1.0;
var bulletPrefab : Transform;
var thingdelay : int;
var cowxpos : float;
var cowypos : float;

function secondwait ()
{
    thingdelay = 1;
    yield WaitForSeconds(2);
    thingdelay = 0;
}

function Update () 
{
    LookAtTarget2 = transform.Find("front");
    cowxpos = transform.position.x;
    cowypos = transform.position.y;    
    
    if (cowxpos >= UFOcontrolBox.ufoxpos + 0.5)
    {
        Shoot();
            
        if(LookAtTarget)
        {
            var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
            
            transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
        }
    }
    
    if (cowxpos <= UFOcontrolBox.ufoxpos - 0.5)
    {
        Shoot();
        
        if(LookAtTarget)
        {
                  rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
            
            transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
        }
    }
}

function Shoot()
{
        if (thingdelay == 0)
        {
            var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
            bullet.gameObject.tag = "enemyProjectile";
            bullet.rigidbody.AddForce(transform.forward * 350);

            secondwait();
        }

}

This is the control script for the gun - thanks in advance everybody!

Quaternions are hard!

What I would do is save the variables and clamp them ( using Mathf.Clamp() ) when you set transform.rotation… But that might not ideal. Anyway, you could give that a try :slight_smile:

ah thank you, but i have no idea how! could you show me an example or direct me to a tutorial?

I could try to give you an example… Can’t guarantee that it works though!

//this is where the wanted rotation is calculated
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 

//this is the part I added, which clamps every variable between a minimum and maximum amount
rotate.eulerAngles = Vector3(
Mathf.Clamp(rotate.eulerAngles.x, minX, maxX), Mathf.Clamp(rotate.eulerAngles.y, minY, maxY), Mathf.Clamp(rotate.eulerAngles.z, minZ, maxZ));

//this spherically lerps the rotation from what it is to the wanted rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

thanks, you’ve been really helpful!

Glad I could help! <3

Thanks Adriaan, this code has helped me too.

And I agree, quaternions are hard. :smile: