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.

Resolved Can anyone tell me why I can't get "ControllerColliderHit" to trigger?

Discussion in 'Scripting' started by Kamon145, Sep 11, 2023.

  1. Kamon145

    Kamon145

    Joined:
    Aug 27, 2022
    Posts:
    17
    I'm hoping that I'm just overlooking something silly, but i just cant seem to figure out where I'm going wrong here. Basically I am trying to push a Rigidbody cube with a CharacterController. I have a player object with a character controller, all of the movement is done using the Move action through script, and i have a seperate script attached to the same object that simply handles pushing objects :
    Code (CSharp):
    1. public class PushRigidbodies : MonoBehaviour
    2. {
    3.     [Header("Basics")]
    4.     public LayerMask pushLayers = new LayerMask();
    5.     [Range(0.5f, 5f)] public float strength = 1.1f;
    6.  
    7.     public void Reset()
    8.     {
    9.         pushLayers = LayerMask.GetMask("Default");
    10.     }
    11.  
    12.     public void PushRigidBodies(ControllerColliderHit hit)
    13.     {
    14.         Debug.Log("Collision Detected!");
    15.         var body = hit.collider.attachedRigidbody;
    16.         if (body == null || body.isKinematic) return;
    17.  
    18.         var bodyLayerMask = 1 << body.gameObject.layer;
    19.         if ((bodyLayerMask & pushLayers.value) == 0) return;
    20.  
    21.         if (hit.moveDirection.y < -0.3f) return;
    22.  
    23.         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0.0f, hit.moveDirection.z);
    24.  
    25.         body.AddForce(pushDir * strength, ForceMode.Impulse);
    26.     }
    27. }
    28.  
    However, the debug log is never called when running into objects on the specified layer and they behave as impassable walls.. The cubes I am trying to push have both a box collider (not set to trigger) & a Rigidbody component (Using gravity, not kenematic), and they can be pushed and moved by other Rigidbodies just fine. I hope all of that makes sense and if I left out any important details please let me know, thanks!!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,228
    Well what's calling this method? It's not a Unity message. It should be called OnControllerColliderHit if you want Unity to call it.
     
    Bunny83 likes this.
  3. Kamon145

    Kamon145

    Joined:
    Aug 27, 2022
    Posts:
    17
    ..oh my gosh I knew it was something silly like that!! I totally forgot to call the script from my main object >_< its working as expected now.. Thank you!!
     
    Bunny83 likes this.