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 /
  • Help Room /
avatar image
0
Question by Omega3939 · Mar 27, 2017 at 11:18 PM · colliderraycasthit

RaycastHit.point always origin

I was attempting to spawn prefabs on a mask mesh which is over a sphere. The mask mesh is simply a sphere with the top and bottom sliced off, the idea being the prefabs will be buildings on a planet and I don't want them to show up in the ocean or weird places like the polls.alt text

Here's the code I'm using:

 public class SpawnIndustries : MonoBehaviour {
 
     public GameObject prefab;
 
     // Use this for initialization
     void Start () {
         spawn(prefab);
     }
 
     public RaycastHit GetPointOnMesh()
     {
         float length = 100.0f;
         Vector3 direction = Random.onUnitSphere; Debug.Log(direction);
         RaycastHit hit;
         Physics.Raycast(transform.position, direction, out hit, length, 1<<8);
 
         return hit;
     }
 
     public void spawn(GameObject p)
     {
         RaycastHit randomPoint = GetPointOnMesh();
         Instantiate(p, randomPoint.point, Quaternion.identity);
         Debug.Log(randomPoint.point);
     }
 }
 

However when the code runs it will always spawn my prefab at the origin. alt text I'm not sure what could be causing this. The mask is on the correct layer (8) and the sphere itself has no collider which could be interfering with the script. Not sure what I could be doing wrong.

debug-output.png (6.2 kB)
collider.png (115.7 kB)
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
1
Best Answer

Answer by Omega3939 · Mar 28, 2017 at 12:52 AM

I figured it out! The problem was Rays that originate inside a collider wont hit anything. The solution was to make the ray originate OUTSIDE the sphere and then shoot it in the opposite direction toward the center.

 public void spawn(GameObject p)
     {
         float length = 100.0f;
         Vector3 direction = Random.onUnitSphere; Debug.Log(direction);
         RaycastHit hit;
         if (Physics.Raycast(transform.position + direction*length, -direction, out hit, length, 1 << 8)) //starts outside and goes in opposite direction
         {
             Instantiate(p, hit.point, Quaternion.identity);
             Debug.Log(hit.point);
         }
     }

Leaving this here if someone comes across this. Sorry for wasting disk space.

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

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

108 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

Related Questions

How can I select 2 different objects using RayCastHit? 0 Answers

Make raycast ignore hitbox? 0 Answers

Detect if Raycast hits a game object 2 Answers

3D side scroller following Sebastian Lague platformer controller 0 Answers

C# Error while trying to set up shot damage and headshots 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