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 aleksanderpiciga · Jan 23, 2020 at 10:46 PM · collisionraycastcollidermeshblender

Why are my Physics.RayCast not showing the collsion with the Ground?

Hello! I have an issue where my RayCast always returns (0,0,0) as point of collision. I am trying to set random positions of enemies (x and z) and place them under the map at a set y value and then cast a ray upwards to detect where it collides with the Ground (I am using Blenders generated plane which isn't convex but i have made sure to enable Mesh Colliders and have checked that they do display correctly). When i cast a ray i always get a result of (0,0,0) as collision point with the ground or in other words no collision. Sometimes howerve i got collision points but only for 1 out of 50 enemies. I have also drawn the same rays i am casting using Debug.DrawRay and they do intersect with the Blender's Plane. They only thing i can think of is that Ground colliders is simply too thin? I have also checked multiple layerMakss values (such as all and none) and have set Ground's layerMask to Ground.

CODE (for loop where i generate enemies and try to get collision point between new enemy and the ground I'm not sure if this is the best way to spawn enemies but i think it should work so i would be very thankful if anyone has any idea what have i done wrong. THANKS A LOT!

 for (int i = 0; i < numOfBasicEnemies; i++)
         {
             GameObject newEnemy = Instantiate(basicEnemy, enemiesParentObject);
 
             // Set new position of an enemy
             Vector3 tempPos = new Vector3(0,-2 * mapHeight, 0); // Set position under the map 2* mapHeight under the map (under y = 0)
             tempPos.x = Random.Range(-xSpawnLimit, xSpawnLimit);
             tempPos.z = Random.Range(-zSpawnLimit, zSpawnLimit);
 
             Vector3 rayOrigin = tempPos + new Vector3(0, 20, 0); // Start the ray a bit above the enemy
             Vector3 rayDirection = Vector3.up; // Point the ray upwards
 
             RaycastHit rayCastHitInfo; // Ray call back object
             Physics.Raycast(rayOrigin, rayDirection, out rayCastHitInfo, 4 * mapHeight, layerMask);
                                                                         // ^^ -> cast ray 2 * mapHeight above the map (above y = 0)
             Debug.DrawRay(rayOrigin, rayDirection * (4 * mapHeight), new Color(255, 0, 0), 100f, true);
 
             Vector3 groundPoint = rayCastHitInfo.point;
             Debug.Log(groundPoint);
            
             newEnemy.transform.position = tempPos;
 
             newEnemy.GetComponent<basicEnemy>().color = gameVaraibles.colorOptions[Random.Range(0, gameVaraibles.colorOptions.Length)];
             //newEnemy.GetComponent<Rigidbody>().useGravity = true;
             newEnemy.GetComponent<basicEnemy>().loadEnemy();
 
             newEnemy.GetComponent<CapsuleCollider>().enabled = true;
         }
         basicEnemy.SetActive(false);

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 NVJOB · Jan 23, 2020 at 11:37 PM 3
Share

This is a fully working script (this is an example).

You need to look at lines 22 - 23, there lies the method you need.

Explanatory video - https://www.youtube.com/watch?v=b9Hhgz$$anonymous$$uYx$$anonymous$$

 using UnityEngine;
 
 public class Spawn : $$anonymous$$onoBehaviour
 {
     public GameObject enemy;
     public float radiusSpawn = 10;
     public int numOfBasicEnemies = 10;
     public Layer$$anonymous$$ask groundLayer;
     Transform tr;
     RaycastHit hit;
 
     void Awake()
     {
         tr = transform;    
     }    
 
     void Start()
     {
         for (int i = 0; i < numOfBasicEnemies; i++)
         {
             Vector2 randomCircle = Random.insideUnitCircle * radiusSpawn;
             Vector3 v3rc = new Vector3(tr.position.x + randomCircle.x, 100, tr.position.z + randomCircle.y);
             if (Physics.Raycast(v3rc, Vector3.down, out hit, 150, groundLayer))
             {
                 GameObject enemyRay = Instantiate(enemy);
                 enemyRay.transform.SetPositionAndRotation(new Vector3(v3rc.x, hit.point.y + 2.0f, v3rc.z), tr.rotation);
                 enemyRay.transform.parent = tr;
             }
         }
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

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

Raycast collisions being detected along a non-existent curve(?) 1 Answer

Restrict held object movement 0 Answers

How to make solid trees? 1 Answer

Raycast hit or miss in the same situation 1 Answer

Create a custom polygon collider based on overlapping ogjects 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