• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Create a custom polygon collider based on overlapping ogjects 0 Answers

My character falls through the mesh collider ground.,My character falls straight through my mesh collider ground. 0 Answers

How can I fix my problem with mesh colliders??!!! 1 Answer

How to fix Unity 5 Mesh Collider Convex Problems? 1 Answer

Make raycast ignore hitbox? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges