- Home /
Detect gameObjects within range and assign them as movetarget
Hello!
I am making a towerdefence game and atm trying to make my enemy AI script be abit smarter. I want the AI to when spawned start moving towards the "Tower" tag but when encountering "MiniTower" tag with spherecollider it should change target and move towards that target instead. They are atm just moving towards the "Tower" tag and ignoring anything on the way. Here is the scripts:
private var anyTargetLeft;
private var target : GameObject;
public var targetPos : Vector3; //to be able to jump(why its public)
public var damage : int = 15;
private var findTarget : boolean = true;
public var moveSpeed : float = 10;
private var grounded : boolean = true;
function Start () {
//animation.Play("Walk");
this.enabled = true;
}
function Update () {
this.gameObject.transform.Translate(Vector3(0,0,Time.deltaTime * moveSpeed));
//Destroys this script so it wont lagg when you loose
var anyTargetLeft = GameObject.FindGameObjectWithTag("Tower").gameObject;
if(anyTargetLeft == null){
Destroy(this);
}
target = this.gameObject.transform.root.gameObject.GetComponent(EnemyDetector).targetSphere;
if(target.gameObject == null){
findTarget = true;
}
if(findTarget == true){
if(target == null){
target = GameObject.FindGameObjectWithTag("Tower").gameObject;
//findTarget = false;
}
//findTarget = false;
}
targetPos = Vector3(target.gameObject.transform.position.x,this.gameObject.transform.position.y,target.gameObject.transform.position.z);
this.gameObject.transform.LookAt(targetPos);
}
function OnCollisionEnter(targetHit : Collision){
//main tower
if(targetHit.gameObject.tag == "Tower"){
var objectHit = targetHit.gameObject;
objectHit.GetComponent(HPTower).towerApplyDMG(damage); //gånger gamelvl senare
Destroy(this.gameObject);
}
//small towers
if(targetHit.gameObject.tag == "MiniTower"){
objectHit = targetHit.gameObject;
objectHit.GetComponent(MiniTowerHP).towerApplyDMG(damage); //gånger gamelvl senare
Destroy(this.gameObject);
}
}
And this one is detecting towers if they come within the spherecollider and then telling that they are the target:
public var targetSphere : GameObject;
function OnTriggerStay(hitTarget : Collider){
if(hitTarget.collider.gameObject.tag == "Tower" || hitTarget.collider.gameObject.tag == "MiniTower"){
targetSphere = hitTarget.gameObject;
}
}
Answer by MikeNewall · May 19, 2013 at 11:35 AM
You could have a sphere collider set to a trigger. Its radius would be set to the distance from the tower that you want your AI to start attacking it.
You need to make sure you tag your enemies, then when something enters the trigger collider you can check if it's an enemy, and if so change its target.
Something like this:
void OnTriggeEnter(Collider other)
{
// Check if an enemy entered the trigger
if(other.gameObject.tag == "Enemy")
{
//Set the new target here
other.target = blahhhh;
}
}
I did that already :D check all the way down in my question. Btw how is void different from a public function, what does it do in comparison? I have never really understood that.
Sorry my answer was in c# not java script! But Void just means that the function returns nothing. And yeah, I should read haha.
Seems a silly question but do you have the collider set to be a trigger? Also to detect a collision you need to make sure at least one of the objects has a rigidbody on it. It's easy to forget.
Using OnTriggerStay will keep setting the target as long as the enemy is in the towers trigger. You should use on trigger enter. The only other thing I can think of is that you may have overlappng colliders?
Your if statement:
if(hitTarget.collider.gameObject.tag == "Tower" || hitTarget.collider.gameObject.tag == "$$anonymous$$iniTower")
The first clause checks for a tower, so if the colliders are overlapping it will always check for the tower first, and if it finds it disregard the $$anonymous$$itower because of the or operator.
;D I am putting this detector script with the OnTriggerEnter function on a parent to this moving target/enemy and this parent only works as a detector to detect towers, with a spherecollider set to on trigger, only the child of this gameobject has a rigidbody however, will this work?
Your answer
Follow this Question
Related Questions
Enemy AI help how to restart collision detection? 3 Answers
Make AI follow player without jumping about 0 Answers
How would I confine an object to the ground? 2 Answers
AI help people 1 Answer
Send an enemy back to its spawn point using waypoints 2 Answers