Why the raycast hit some objects and some not ?

In the Hierarchy I have a Space Station and under the space station there are 5 children.
In one of the children children inside there are many more children some of them are name: Roof_Normal_01

While the game is running I’m using the mouse cursor and the ray cast to hit and spot light the objects. And the raycast detect and hit the Roof_Normal_01 objects. For example:

But it’s never detecting/hitting other objects there are like over 890 objects in the space station but it’s hitting only some of them.

And the ones it’s never hitting are the ones under the child _Level:

I can’t figure out why it’s never hitting the objects under _Level

This is the script with the raycast:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Map : MonoBehaviour
{
    public Camera mapCamera;
    public Text objectWasHit;
    public GameObject mouseOvered;
    public float distanceToHit = 1000;

    private Camera[] cameras;
    private List<GameObject> objectsToHit = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        cameras = Camera.allCameras;

        GameObject levels = GameObject.Find("_Level");
        foreach (Transform child in levels.transform)
        {
            child.gameObject.AddComponent<UnityEngine.MeshRenderer>();
            child.gameObject.AddComponent<BoxCollider>();
            objectsToHit.Add(child.gameObject);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapCamera.enabled == false)
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = false;
                }
                mapCamera.enabled = true;
            }
            else
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = true;
                }
                mapCamera.enabled = false;
            }
        }

        bool rcHit = false;
        Vector3 mouse = Input.mousePosition;
        Ray castPoint = mapCamera.ScreenPointToRay(mouse);
        RaycastHit hit;
        Debug.DrawRay(castPoint.origin, castPoint.direction * distanceToHit, Color.magenta);
        if (Physics.Raycast(castPoint, out hit, distanceToHit))
        {
            rcHit = true;
            if (mouseOvered != hit.collider.gameObject)
            {
                mouseOvered = hit.collider.gameObject;
            }

            foreach(GameObject go in objectsToHit)
            {
                if (hit.collider.name.Equals(go.name))
                    objectWasHit.text = mouseOvered.name;
            }
            objectWasHit.text = mouseOvered.name;
        }

        if (!rcHit && mouseOvered != null)
        {
            mouseOvered = null;
        }
    }
}

It’s never reaching to this line:

objectWasHit.text = mouseOvered.name;

Using a break point showing it’s never hitting the objects that are children of _Level and many other objects in the space station but for now I need to hit the objects under _Level.

In the Start I’m adding to the objects under _Level a MeshRenderer and BoxCollider but this didn’t solve it either.

The script is attached to the Map Camera:

Well I can’t see the objects that are children of Corrider_Window_part_05, but the guesses that would come to mind:

  1. Do these objects have colliders?
  2. If the colliders are mesh colliders, are they facing the right way?
  3. What layer are these objects on?

Line 59, you want to change it to RaycastAll, then look at all the hits for what you’re looking for. The raycast you’re doing only returns the first object that was hit. IE the roof object.

I love the space station models. keep up the good work.

1 Like

Did you get the hit box increased?