Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Apr 15, 2018 at 04:34 AM · raycastvector3randomsphere

Raycasting onto a sphere

Hi,

I'm attempting to spawn objects on a random point on a sphere. To get this point, I am calling Random.onUnitSphere and multiplying that around 70 to make sure it's outside of the sphere. My sphere is located in the centre of the world so, I then call Physics.Raycast(randomSpherePoint, Vector3.zero, out hit). However, this never seems to be hitting anything and debug lines do not get drawn either. I have made sure that my sphere has a collider, too.
This is my full code:

 for (int i = 0; i < 10; i++)
 {
     Vector3 randomSpherePoint = Random.onUnitSphere * 70;
     RaycastHit hit;
     Debug.DrawRay(randomSpherePoint, Vector3.zero, Color.white, 120);
     if (Physics.Raycast(randomSpherePoint, Vector3.zero, out hit))
     {
         print("HIT!");
         Instantiate(astroid, hit.transform);
     }
 }
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 Bunny83 · Apr 15, 2018 at 04:46 AM

A Raycast takes a start position in worldspace and a direction vector. You pass Vector3.zero as direction which is no direction at all. To get the direction vector from your point to the origin it's actually just the inverse of the position

 if (Physics.Raycast(randomSpherePoint, - randomSpherePoint, out hit))

If the sphere is not located at (0,0,0) but at some "center" you would have to do:

 if (Physics.Raycast(center + randomSpherePoint, center - randomSpherePoint, out hit))

However if you have an actual target point you may want to use a LineCast instead. The LineCast actually expects two worldspace points, a start and an end point.

Comment
Add comment · Show 5 · 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 $$anonymous$$ · Apr 15, 2018 at 04:57 AM 0
Share

@Bunny83 Thanks! Quick question, my casts seem to be intersecting at the centre as shown: https://i.imgur.com/0qlyRdZ.png
Whereas I want them to hit the green mesh. $$anonymous$$esh has a collider, and casts are made after the mesh is made, so I'm not sure why it's not hitting my mesh.

avatar image Bunny83 $$anonymous$$ · Apr 15, 2018 at 05:23 AM 0
Share

Well, do you have a $$anonymous$$eshCollider on that object? Raycast and LineCast can only hit colliders, not renderers.

avatar image $$anonymous$$ Bunny83 · Apr 15, 2018 at 12:39 PM 0
Share

@Bunny83 Yeah, it definitely has a collider, as shown here:
https://i.imgur.com/bp8wGZw.png

Show more comments

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

126 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 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 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 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 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 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 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

Randomly Instantiating an object inside a shrinking sphere? 1 Answer

Spawning objects to the surface of a spehere correctly. 0 Answers

Generate a random direction within min and max angles 2 Answers

Spawning at a random position away from the player 1 Answer

Why does my Vector3.SmoothDamp smoothtime variable affect the target position instead of the actual smoothtime? 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