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 ninjaboynaru · Mar 25, 2013 at 04:45 PM · javascriptcollisionrts

RTS range detection

I have an RTS game with a few units called tank and a hostile enemy.

How would I go about detecting when an enemy is within range of one of my units so they can turn and fire on it.

JavaScript please

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ninjaboynaru · Mar 25, 2013 at 10:38 PM

Finally after 2 hours of trying I got it. Physics.SphereCat solved the problem.

 var hits : RaycastHit;
 if(Physics.SphereCast(transform.position, 50, transform.forward, hits, 0.1)){
 Debug.Log(hits.collider.tag);
 }

Now I just need to add the enemy to an array and figure out how to get the tank to pick which enemy and attack it, not to mention clearing the array when the enemy is no longer in the array.

Thanks for all the help. Though jmatthews or vagos answers did not solve this problem they will be helpfull when i get to creating other parts of the game.

Comment
Add comment · 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
1

Answer by jmatthews · Mar 25, 2013 at 05:50 PM

I can tell you conceptually and with some pseudo code...

You'll want each of your tank(game objects) to have a sphere collider which is marked as a Trigger. The you'll want a method that looks similar to this:

void OnTriggerEnter(Collider other) { if(other.tag == "enemy") { target = other.transform; }

}

where each potential enemy has an "enemy" tag in the editor, and target is a transform that you'll use to aim the tank's weapons with.

One more detail, in order for the TriggerEvent to fire one of the two objects must have a rigid body attached to it.

Comment
Add comment · Show 2 · 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 ninjaboynaru · Mar 25, 2013 at 05:54 PM 0
Share

Tried but not possible as I need to be able to click on the tanks original collider to add it to the selectedUnits que

avatar image whydoidoit · Mar 25, 2013 at 06:01 PM 0
Share

Have a second collider on a child object that is on a different layer...

avatar image
1

Answer by vagos21 · Mar 25, 2013 at 05:57 PM

the way i do it for my tower defense game is to use sphere colliders with the range i want them, since they're faster than any other collider primitive. all you have to do is 1. add sphere collider to your units (set it to trigger) 2. add sphere collider to enemy units (set it to trigger) 3. add a rigidbody to either your unit or the enemy, and set it to kinematic (needed for the collision events to work) 4. create a tag for enemy units, for example called "enemyunit" and assign it to all your enemy units 5. assign a script like this one to your units:

 private var goUnderAttack:GameObject;
 function OnTriggerStay(other:Collider) {
    if (other.tag == "enemyunit"){ //lock on enemies only
       goUnderAttack = other.gameObject;
       Debug.Log(gameObject.name + " is locked on " + goUnderAttack.name);
    }
 }

goUnderAttack will be the gameobject of your enemy that the tower is locked on. then you can do anything you want with it. i use OnTriggerStay instead of OnTriggerEnter, because if other enemy units enter the range and your current locked on enemy dies, they will be the next to attack. if you use OnTriggerEnter, other enemies might enter the range but will not be attacked. i hope this was helpful enough!

Comment
Add comment · Show 1 · 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 vagos21 · Mar 25, 2013 at 06:02 PM 0
Share

sorry the answer was given while i was typing in my own... :D this is interesting to see others work the same way, and i'd also love to know the solution to this :) even though, i think i'd go away with an empty GO that would update its position on every frame to be the same as the unit's position, and set the sphere collider on that "helper" object. then i'd set a tag of "rangecollider" to that GO and only let mouse interact with my unit and not the "rangecollider" tagged GOs...

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

13 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

Related Questions

OnTriggerEnter NullReferenceException 0 Answers

Destroy On Collision will not work with spawned AI 1 Answer

collision script to destroy 2 Answers

two objects colliding 3 Answers

Collide detection with tag [JS] 0 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