• 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 CuddleMonster · Nov 21, 2012 at 07:26 PM · arrayaivector3.distancetargettinggrenade

Scripting Grenade AI. Checking if Array Objects are close to eachother.

I'm working on an AI script and want to include AOE effects such as grenades. The idea.. is that if two or more enemies are within 4meters of eachother, the attack is aimed at one of them... or in the middle (depending on specific attack)

I could manually do this by saying "if Enemy[1] is near enough to Enemy[2] ... shoot inbetween..." but obviously that doesn't work as I have an unset number of playertargets.

Available Targets are listen in an Array... also.. there is a CurrentTarget variable.. so if nothing else I could compare all other members of array to the current targets position.

If it was testing for 3 people in close proximity how would it differ?

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
Best Answer

Answer by sparkzbarca · Nov 21, 2012 at 07:33 PM

heres roughly how you do it

raycastsphere to nearest enemy or whatever your target is. This is just your initial target your not going to FOR CERTAIN use that as the target for the grenade in the end.

That will give you an array which contains all objects within a certain radius of the enemy including the target himself.

Now what you do is you make the target point for the grenade equal to

 gameobject[] enemytargets = physics.raycastsphere(origin is nearest enemy or whatever initial target, radius is your choice)
 
 this will make enemytargets an array containing all the enemies
 
 vector3 targetpoint = vector3.zero;
 
 foreach (gameobject enemytarget in enemytargets){
 targetpoint += enemytarget.transform.position;
 }
 
 targetpoint /= enemytargets.size();
 

what you have done with that code is taken the position of all the enemies and found the center by adding all the positions and dividing by the number of positions.

Now if there is only 1 enemy the center is the enemy so 1 enemy will resolve itself

multiple enemies will have the center point between all enemies found.

Make sure the radius is set right for the explosion.

You wouldnt want the radius so high that you throw the grenade in the middle 4 guys spaced so far apart none of them are hit.

technically speaking this will be a point above the ground. if you want to aim for "ground"

raycast with an origin as the targetpoint, the direction as -vector3.up so you cast straight down.

set the targetpoint to hit.point; that will give you the point on the ground in the center of all targets.

MARK AS ANSWERED and happy coding

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 CuddleMonster · Nov 21, 2012 at 07:51 PM 0
Share

This covers my alternative approach, to see if other enemies are nearest to my target.

Is there a way of checking proximity of all Array members with every other Array member? I believe the Target solution as you have answered might be best for most situations, but as a growing coder I'd like to know all angles.

avatar image sparkzbarca · Nov 21, 2012 at 08:10 PM 0
Share

you could do it, it not could but WOULD get really expensive with a large number of enemies.

enemyarray is also an array of type enemy which is the struct. it includes an list called nearbyenemies which is of type vector3 for this example since all it's doing is storing positions.

alternatly you could have enemyarray be an array of gameobjects and have each enemy gameobject have a script with nearbyenemies declared in it and get the attached component.

this will use gameobject arrays

 foreach (enemy in enemyarray)
 {
   enemiesnearby = enemy.getcomponent<ScriptWithNearbyEnemiesList>().Nearbyenemies;
  foreach (otherenemy in enemyarray)
 {
  if(otherenemy != enemy)
   enemiesnearby.push(otherenemy);
 }
 //insert a sort here if you want
 }

that will give each enemy an array attached to it that stores the position of nearby enemeis

you could iterate though that to get distance or something.

Honestly i'm not sure why you want this but hey its there. :)

mark as answered. :)

avatar image CuddleMonster · Nov 21, 2012 at 08:38 PM 0
Share

Thank you

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Issues NPC Grenade avoidance 3 Answers

Cycle Through Array of Enemies 1 Answer

If one point is taken go the other point. Quick fix? 2 Answers

AI Target Finding Optimization 0 Answers

My AI drops stuffs on death even when not supposed to 1 Answer


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