Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 edwarddewolf · Feb 13 at 12:42 PM · raycast3draycasthit

Why is raycast not ALWAYS working?

I am raycatsing a plane (with collider) 10 times, but for a reason I cannot understand, only a few times a hit is registered and a sphere is generated on that position. I used a Debug.DrawRay to simulate the raycats to amke sure the ray is supposed to hit, but it does not. Is there anyone that can help me?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WorldGenerator : MonoBehaviour
 {
     public int seed = 11;
     public Material planeMaterial;
     public Material sphereMaterial;
 
     void Start()
     {
         Random.InitState(seed);
         AddPlane();
         AddSpheres(10);
     }
     private void AddSpheres(int maxSpheres)
     {
         for (int i = 0; i < maxSpheres; i++)
         {
             float posX = Random.Range(-14, 14);
             float posZ = Random.Range(-14, 14);
 
             Vector3 rayPos = new Vector3(posX, 50, posZ);
 
             RaycastHit hit;
 
             Debug.DrawRay(rayPos, Vector3.down * 100, Color.yellow, 1000);
 
             if (Physics.Raycast(rayPos, Vector3.down, out hit, 100))
             {
                 Debug.Log(i + ": " + hit.distance);
                 GameObject newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); //Instantiate(gameWorld.islandShapes[Random.Range(0, gameWorld.islandShapes.Length)]);
                 newSphere.name = "Sphere_" + i;
                 newSphere.transform.position = new Vector3(posX, 0, posZ);
                 newSphere.transform.localScale = new Vector3(5, 2, 5);
                 newSphere.transform.rotation = Quaternion.identity;
                 newSphere.GetComponent<MeshRenderer>().material = sphereMaterial;
             }
         }
     }
     private void AddPlane()
     {
         GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
         plane.transform.localPosition = new Vector3(0, 0, 0);
         plane.transform.localScale = new Vector3(3, 1, 3);
         plane.GetComponent<MeshRenderer>().material = planeMaterial;
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SellPet · Feb 13 at 03:49 PM

check if the ray is going where you want

 onDrawGizmos(){
     Gizmos.DrawRay(Paste same ray origin and direction);
     }
 

you will see the ray in the viewport. if it does go where you want i would recommend changing collider to cube collider. and see if the problem goes

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

Answer by edwarddewolf · Feb 13 at 04:12 PM

I added a boxCollider to the plane, and now it works perfectly. Is there a reason why the default meshcollider on a plane would not notice all the raycast hits and a boxcolldier does?

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 edwarddewolf · Feb 13 at 06:11 PM 0
Share

Actually, as far as I understand with my limited knowledge, I found the real reason, I got it working with a default meshcollider now too.

The real issue is that I cam doing this from a start function, which has as a side effect that the physics engine might not have caught up with adding all the objects and colliders.

If I disable Physics.autoSimulation, and add Physics.Simulate(1); in between adding each gameobject , all raycasthits are behaving as expected and will collide with all previously placed objects

Do not forget to enable Physics.autoSimulation afterwards

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

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

Quarternion.LookRotation is only rotating my game object on one axis 1 Answer

What is wrong about this RaycastHit Event??? 1 Answer

Boxcast vs Raycast Oddities 1 Answer

Cancel Input if the direction has a wall in the way 1 Answer

RaycastHit always returns 0 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