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
1
Question by Piflik · Nov 19, 2013 at 07:54 PM · c#raycastai

Find number of objects between two points

I am currently trying to write a simple AI and I need a quick way to find the number of objects between two points.

Little more details: It is something like a breakout game, but with tanks fighting each other, with destructible cover in between. Each of the tanks can only move along a vertical line and in order to find a valid attack position for the enemy I want to find a path between the player and a spot on the enemies 'homezone' that minimizes the number of obstacles between the two (with closer positions with slightly more obstacles would be preferred to further positions).

My first instinct would be to shoot some rays from a number of positions (slightly randomized) towards the player, but I have no idea if or how I could count the number of hits, since the Raycast only returns the first. Another idea might be to 'sweep' a collider in an arcing movement across the battlefield, pivot at the player's position and use OnTriggerEnter/Exit to increment/decrement a number and remember the angle when that number was smallest. But I would need many frames to make it work, instead of calculating the best position in a single frame.

Is there an existing function in Unity that would allow me to find the number of objects quickly and reliably, or alternatively a workaround/hack?

Comment
Add comment · Show 1
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 Professor Snake · Nov 19, 2013 at 08:12 PM 0
Share

Not sure how well this would work since it depends on your setup, but you could raycast downwards multiple times along a path between source+Vector3(0,20,0) and dest+Vector3(0,20,0). You'd then store unique hits. Something like:

 var objectsHit:Array=new Array();
 for(var i:float=0;i<=1;i+=0.01){
 var curPos:Vector3=Vector3.Lerp(source+Vector3(0,20,0),dest+Vector3(0,20,0),i);
    if(Physics.Raycast(curPos,-Vector3.up,blah blah){
       //if the hit object isn't on the objectsHit list, add it.
    }
 }

1 Reply

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

Answer by Kiloblargh · Nov 19, 2013 at 08:15 PM

Are you missing RaycastAll? That looks like just what you need here.

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 Piflik · Nov 19, 2013 at 08:20 PM 0
Share

Huh...sometimes the answers are so easy. Thanks.

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

18 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

AI Pathfinding using RayCast 0 Answers

2D Field of View 2 Answers

Best way to do an AI 1 Answer

AI line/field of sight with Raycast 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