Hey guys,
There is an old thread concerning this issue, but I hope to bump this thread up for a better solution. >.<
The link to the old thread is below.
http://forum.unity3d.com/viewtopic.php?t=5036&postdays=0&postorder=asc&start=0&sid=f6023c77a47c561a09edbdbd4388bc0c
I’m trying to make a shameless clone of a tower defense game, a wave of instanced mobs will attempt to cross from point A to B, with turrets placed along the instance points. The problem lies in the turret’s tracking code.
I used the modified sentrygun script from the old thread, unfortuantely I couldn’t get Targos’s GetHim() script version to work. Only the version with the targeting “player” tag moved to update() instead of start().
Here is the script on the turret.
var attackRange = 30.0;
var shootAngleDistance = 10.0;
var target : Transform;
function Update () {
if (target == null GameObject.FindWithTag("player"))
target = GameObject.FindWithTag("player").transform;
if (!CanSeeTarget ())
return;
// Rotate towards target
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
// If we are almost rotated towards target - fire one clip of ammo
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
SendMessage("Fire");
if (target == null GameObject.FindWithTag("player"))
target = GameObject.FindWithTag("player").transform;
}
function CanSeeTarget () : boolean
{
if (Vector3.Distance(transform.position, target.position) > attackRange)
return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
return false;
}
If I have more than one mob spawn point, I noticed all the turrents irregardless of their range will target the same specific mob(from the same spawn point) and ignore the other spawn point around each turret.
Also, unless the targetted mob is destroyed, the turrets will not switch targets, even if they were out of range.
I feel the original sentry script is inedequate to switch targets based on range. Here’s a short list of what the turrets should do.
- Whenever a mob(closest) comes within range of a turrent it will set that mob as the target and fire.
- Once out of range the turret will reset the closest available mob as its target and fire.
- If none is found clear target var and update to check mobs available?
I think the key lies on the script to identify more than one mob at a time. Currently using FindWithTag is constricting for more than one object.
Or is there a ‘FindAllObjectsWithTag()’ component? XD
Thanks!