Physics.Raycast not hitting any objects

Hello,

I am learning to made simple third person game using Unity and as I am have few problems now and them. Right now i can’t get Raycast to work properly. My main goal is to interact with world objects using ray that is casted from character eyes, so I made separate game object inside Player model (child: Eye_Level) and fixed it rotation with camera rotation. When ray is casted through door model Debug.Log (inside Raycast if) doesnt give me any feedback although i see in editor that ray is going through door model. Door model is on default layer and has mesh collider on. CheckInteraction is called in Update() of course, it’s working as DrawRay can be see in editior. Can someone help me figure out why my ray won’t collide with objects?

void CheckInteraction()
    {
        Vector3 origin = eyeLevel.transform.position;
        Vector3 direction = eyeLevel.transform.forward;
        float distance = 4f;
        RaycastHit hit;

        Debug.DrawRay(origin, direction);

        if(Physics.Raycast(origin, direction, out hit, distance))
        {
            Debug.Log("It worked 1");

            if (hit.transform.tag == "Door")
            {
                Debug.Log("It worked 2");
                if (Input.GetKeyDown(KeyCode.E))
                {
                    hit.transform.gameObject.GetComponent<doorOpen>().enabled = true;
                }
            }
        }
    }

Your door has no collider. If you want a working meshcollider, you have to set it’s mesh property in the inspector. I would use that boxcollider you have on there and delete the meshcollider though.

1 Like

Thank you very much! it worked, I knew it would be something simple. :slight_smile: