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 Timurov · Dec 19, 2012 at 09:23 AM · physicsraycastdistancespherecast

How to get distances of GameObjects through SphereCast?

I've a list of gameobjects and I need to get a distance between gameobjects through RaycastHit. Is there a way to get unique distance for each gameobject looking to each other?

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

Answer by ShadowAngel · Dec 19, 2012 at 10:22 AM

just use Vector3.Distance. go with For thro each gameObject with thet list you have.

You gona do it like this:

 var MyList : ArrayList; // This is your list of GameObjects.
 var MyDistanceList : ArrayList; //This is empty list for storeing our distances.
 
 function GetDistance()
 {
 for (var obj : GameObject in MyList)
 {
 MyDistanceList.Add(Vector3.Distance(transform.position, obj.transform.position)); //Add vaule to MyDistanceList on same index position as our target object in MyList.
 }
 }
 

So here you go a script thet creates list of distances of all gameObjects on your list with curnet one thet this script is attached to. Dont forget to use this function in Update or Start.

Comment
Add comment · Show 4 · 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 Owen-Reynolds · Dec 19, 2012 at 04:29 PM 0
Share

To reiterate, Raycasts (and SphereCasts) are when you don't know the objects. They ask "what's in that direction?"

Like if you want to check for falling, can raycast down to see if a car, a rock, the ground or whatever is below you. But, If you don't have cars and rocks, so you know the ground is the only thing below you. you can just look up the ground height where you are -- no raycast needed.

avatar image Timurov · Dec 20, 2012 at 11:38 AM 0
Share

@ShadowAngel, Sure, probably should work properly, but I need to get Distance, Collision and Direction, thats why I've to use RaycastHit, actually Physics.SphereCast to cover all the data I need from the list of game objects.

avatar image Timurov · Dec 20, 2012 at 11:45 AM 0
Share

@Owen Reynolds, but when I need to get the distance, collision, direction. I think there is only the way to use SphereCasts for grabbing those data from game objects or you can propound any other approaches?

avatar image Owen-Reynolds · Dec 20, 2012 at 02:41 PM 0
Share

"get Direction"? You have to already know the direction in order to do a *cast.

To find direction to a known object, can use Quaternion.LookRotation(pos2-pos1);

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

11 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

Related Questions

Is Raycast relatived to screen resolution? 1 Answer

SphereCastAll doesn't seem to be working 2 Answers

Help with Physics.BoxCast (or maybe ray casts in general?) 1 Answer

Raycast stop at distance 3 Answers

Is it possible to capture a 'free' parent transform, make it into a child, and then move it with the parent? 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