collision not recognized

I gave the player a collider to play role of hand 9730300--1391278--upload_2024-3-27_7-33-0.png
If a collision happened between the hand and the truck and player pressed E the player should be able to drive the truck but the weird thing is that sometimes the truck recognize the collision sometime not?
why?


OnCollisionStay updates at the same rate as FixedUpdate and so GetKeyDown will be unreliable because it’s meant to be used in Update.

A rigidbody will quickly go to sleep if it’s not moving and will no longer trigger OnCollisionStay.

2 Likes

Please stop making duplicate posts for the same issue; this is the 3rd post. Please create a single thread for working on something.

I’ll close the other thread because you don’t show anything related to the ECS system. If it is ECS then you’re in the wrong forum with this duplicate post.

Thanks.

I added a cube only have a BoxCollider to test it with the switch " https://discussions.unity.com/t/943297 " but the collision is not detected by the switch
9739717--1393222--upload_2024-3-30_13-59-25.png
switch script
9739717--1393225--upload_2024-3-30_13-59-43.png
you said that collision don’t work with the character controller but I have this script for a car and its working fine
9739717--1393228--upload_2024-3-30_14-1-9.png

Its Look like I don’t undrstand how this onCollision… work
the code you give me is working find thanks

void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Collider[] colliders=Physics.OverlapSphere(transform.position,10); // get the nearby colliders
            foreach(Collider c in colliders)
            {
                if (c.gameObject.CompareTag("lamp"))
                    c.gameObject.SetActive(!c.gameObject.activeSelf);
            }
        }
    }

It works because there’s a rigidbody present. OnCollision is for rigidbody related collisions but it doesn’t work with character controller. OnTrigger works with both character controller and rigidbody.

Or if you want to detect collisions with solid objects when using the character controller then you should use:

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Debug.Log(hit.collider.name);
    }

It’s all very confusing. :face_with_spiral_eyes:

1 Like

Thanks for your help