Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Raycasting colliders without rigidbodies

Discussion in 'Editor & General Support' started by Digitex1, Jan 11, 2023.

  1. Digitex1

    Digitex1

    Joined:
    Aug 28, 2017
    Posts:
    41
    Hello,

    I've got a weird issue in a new project: I used to raycast objects without rigibodies from my First-person view rigidbody character controller:

    Code (CSharp):
    1. Physics.Raycast(new Ray(camerRayOrigin, transform.forward), out RaycastHit hit, rayDistance, interactLayers,QueryTriggerInteraction.Collide)
    However, in my new project this wouldn't work until I add a rigidbody to the target collider.

    has anyone have an idea about what could be the issue?

    2/ I thought about adding a KINEMATIC rigidbody to each target collider (they are usually static colliders). But, I wonder what is the performance impact of such solution? I'm targeting low end devices like mobile devices.

    Kind regards
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Raycasts should have no problem hitting colliders with or without a Rigidbody. The Rigidbody isn't really relevant. Maybe share a little more context about what you're doing here and some more code? For example how is your layermask set up? What layer(s) are the collider(s) you are trying to hit on? Etc..
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,765
  4. Digitex1

    Digitex1

    Joined:
    Aug 28, 2017
    Posts:
    41
    thanks for the confirmation, I found the issue, it all comes down to using hit.transform.gameObject instead of hit.collider.gameObject :rolleyes: in a context where the target collider is a child of a root object that has a kinematic rigidbody