Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fetdressing · May 19, 2013 at 10:15 AM · aiontriggerstayspherecollider

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fetdressing · May 19, 2013 at 02:06 PM 0
Share

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.

avatar image MikeNewall · May 19, 2013 at 09:55 PM 0
Share

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.

avatar image Fetdressing · May 20, 2013 at 12:02 PM 0
Share

;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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges