Spherical Turret

The following script is something I have used in a prototype “Earth Defense” type game. It enables a Planet to have a cannon/gun that can fire in all directions at a predetermined tagged object. To set it up, here are some points to follow:

– Firstly create a sphere. For arguments sake, this will be a Planet, so name it Planet.
– Create another object (empty or perhaps a cube) and name it “gunTip”. Make this object a child of the Planet and position it on the outside of the Planet. Ensure that the gunTip has no collider or rigidbody… It only needs a mesh filter and renderer if you want it visible.
– Now attach the script below to the Planet.
– Once attached, there are some variables to setup.
– Search Tag – This is the tag assigned to the objects you wish to fire at.
– Bullet Prefab – Assign a bullet to this. Now you may wish to use a small sphere or a cube, but make sure you have a collider and rigidbody attached to the bullet.
– Other variables like Damp, Shoot Speed and Saved Time are all up to you on how they are set. Change them to meet your needs.

Now all you need to do now is create an enemy type object and assign the Search Tag to it.

// the tag to search for (set this value in the inspector)
var searchTag = "Shoot";

// the frequency with which to re-scane for new nearest target in seconds
var scanFrequency = 1.0;

// the current target
private var target : Transform;

var savedTime = 0;
var bulletPrefab: Transform;
var damp = 3.0;
var shootSpeed = 2000;

function Start() {
    // set up repeating scan for new targets:
    InvokeRepeating("ScanForTarget", 0, scanFrequency );
}

function Update() {
    // we rotate to look at the target every frame (if there is one)
    if (target != null)
    {

        var rotate = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
		if(target)
		{
			var seconds : int = Time.time;
			var oddeven = (seconds % 2);
			if(oddeven)
			{
				Shoot(seconds);
			}
		}
    }
}

function ScanForTarget() {
    // this should be called less often, because it could be an expensive
    // process if there are lots of objects to check against
    target = GetNearestTaggedObject();

}

function GetNearestTaggedObject() : Transform {
    // and finally the actual process for finding the nearest object:

    var nearestDistanceSqr = Mathf.Infinity;
    var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
    var nearestObj : Transform = null;

    // loop through each tagged object, remembering nearest one found
    for (var obj : GameObject in taggedGameObjects) {

        var objectPos = obj.transform.position;
        var distanceSqr = (objectPos - transform.position).sqrMagnitude;

        if (distanceSqr < nearestDistanceSqr) {
            nearestObj = obj.transform;
            nearestDistanceSqr = distanceSqr;
        }
    }

    return nearestObj;
}

function Shoot(seconds)
{
	if(seconds!=savedTime)
	{
		var bullet = Instantiate(bulletPrefab, transform.Find("gunTip").transform.position, Quaternion.identity);

		bullet.rigidbody.AddForce(transform.forward * shootSpeed);

		savedTime = seconds;
	}
}

Advertisements

About AK Studio Development

We are a small indie development group from New Zealand. We have been dabbling in iPhone app and game development since 2010 and are continuously learning new tricks. As we learn, we create. We look forward to sharing all our ideas through our current and future apps! AK Studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: