Home > Mobile >  OnCollisionEnter is not called
OnCollisionEnter is not called

Time:07-09

I need some help with Unity. I try to make an object to detect a collision, but for some reason, the OnColissionEnter function is not called. Both my objects have rigidBody and Box Collider attached, isTrigger is unenabled as well. I supposed it's because I have AddForce in my code, but I am not sure. Have anybody a clue what is going wrong there?

Here is the code:

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

public class Cuplat2 : MonoBehaviour
{
    public Vector3 Target = new Vector3();
    private Rigidbody rb;
    public float thrust;


    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        

    }
   
    void FixedUpdate()
    {
        Vector3 NewPosition = transform.position   Target;
        Vector3 Position = transform.position;

        if (transform.position.x < Target.x)
        {
            rb.AddForce(transform.position * thrust);
        }
        else
        {
            //rb.isKinematic = true;
            

        }

    }
   public void OnCollisionEnter(Collision collision)
    {
        Debug.Log("stop");

        if (collision.gameObject.name == "Cupla T2") 
        {
            Debug.Log("stop");
        }
    }

}

enter image description here

CodePudding user response:

One of your rigidbodies has to have isKinematic = false;

If it's not possible in your case I see they have workaround for that:

https://forum.unity.com/threads/collision-detection-for-kinematic-rigidbodies.885778/

Other option would be that the collision is disabled in collision layer mask, but that would cause objects to pass trough each other (I believe that's not the case)

Third option: your rigidbody does not have collider (maybe collider is on parent, has enabled = false, or inactive game object?)

Fourth option: If your object moves very fast you should change Collision Detection Mode to Continous Dynamic

CodePudding user response:

How about the collision layer matrix? Go into Edit > Project Settings, then select the Physics. Check if the layers set to the GameObjects are actually having any collision between them. Collision Layer matrix

  • Related