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

on collision enter not working

Discussion in 'Scripting' started by megatron0, Jul 29, 2019.

  1. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    there is no debug message or anything
    ontrigger is false on both
    i have rigid body on both
    collider on botb
    made sure to spell it right 10 times
    i even tried ontriggerenter still not working
    when the cube collides nothing happens

    void OnCollisionEnter(Collision col){
    Debug.Log("hit");
    }
     
  2. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    One of the rigidbodies needs to be non-kinematic for it to work. Have you check that?
    Also, check the collision matrix and see if the layers the objects are in can detect collisions with each other.
     
    Joe-Censored likes this.
  3. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    yeah both are non kinematic and same layer which is default
     
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    your game is 3D, right?
     
  5. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    yeah
     
  6. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    What type of colliders are you using? Are you using a mesh collider or a regular one? Is any of the objects marked as static? Do they ever register a collision or they just fail sometimes? Does your script inherits from monobehaviour? Have you tried with other objects and other layers? Have you try to see if other functions like collision stay are at least called? Does the object collide to the other (not letting it pass or moving it if you try to go against it).
     
  7. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
  8. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    they r box colliders
    yes it is monobehavior
    no i didnt try other layers nor other collision
    i tried once they let collide but i changed gameobjects now they pass
    both cases didnt work tho
     
  9. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
  10. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    If non works, try to give more info. Test it in a new empty scene, post images with your hierarchy and components, give more info on how you're moving the objects during play mode. Maybe they're beign moved too fast and Unity can't compute it. There are phyisics options that can check faster collisions. If they're not generating a reaction with each other, you should see why this is happening.
     
  11. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    ok thanks i will try those.
     
  12. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I have a new test project in which I have a sphere with a rigidbody in a static world of box and mesh colliders.

    If the sphere leaves the ground and hits another object on its way down it will not trigger OnCollisionEnter() again making it impossible to jump. I am logging the OnCollisionEnter() and OnCollisionExit() and it seems OnCollisionExit() is called more often than OnCollisionEnter(). In the image attached you can see the sequence of events. The sphere is resting on the collider, but OnCollisionEnter() has not been called. ruhroh.PNG

    This is Unity 2019.3.0f6.

    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleMoveRigidbody : MonoBehaviour
    6. {
    7.     private Rigidbody _body;
    8.  
    9.     private Transform _camerae;
    10.  
    11.     private Vector3 _startPosition;
    12.  
    13.     private float _force = 3.0f;
    14.  
    15.     private bool _canJump = true;
    16.  
    17.     void Start()
    18.     {
    19.         _startPosition = transform.position;
    20.         _body = GetComponentInChildren<Rigidbody>();
    21.         _camerae = FindObjectOfType<Camera>().transform;
    22.     }
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         if (_canJump)
    28.         {
    29.             Vector3 direction = (_body.position - _camerae.position).normalized;
    30.             _body.AddForce(direction * (Input.GetKey(KeyCode.W) ? _force : Input.GetKey(KeyCode.S) ? -_force : 0.0f) + Vector3.Cross(Vector3.up, direction) * (Input.GetKey(KeyCode.D) ? _force : Input.GetKey(KeyCode.A) ? -_force : 0.0f), ForceMode.Force);
    31.             _body.AddForce(Vector3.up * _force * (Input.GetKeyDown(KeyCode.Q) ? _force : 0.0f), ForceMode.Impulse);
    32.         }
    33.         else
    34.         {
    35.             _body.AddForce(Vector3.up * _force * (Input.GetKey(KeyCode.E) ? -_force : 0.0f), ForceMode.Impulse);
    36.         }
    37.  
    38.         if (Input.GetKeyDown(KeyCode.R))
    39.         {
    40.             _body.velocity = _body.angularVelocity = Vector3.zero;
    41.             _body.position = _startPosition;
    42.         }
    43.     }
    44.  
    45.     private void OnCollisionEnter(Collision collision)
    46.     {
    47.         _canJump = true;
    48.  
    49.         Debug.Log(_canJump + " | " + collision.collider.name);
    50.     }
    51.  
    52.     private void OnCollisionExit(Collision collision)
    53.     {
    54.         _canJump = false;
    55.  
    56.         Debug.Log(_canJump + " | " + collision.collider.name);
    57.     }
    58.  
    59.     private void OnCollisionStay(Collision collision)
    60.     {
    61.         //Debug.Log(collision.collider.name);
    62.     }
    63. }
    Please point out what I am doing wrong.

    I have a workaround here:

    Code (CSharp):
    1.  
    2.     private void OnCollisionEnter(Collision collision)
    3.     {
    4.         ++_collisions;
    5.     }
    6.  
    7.     private void OnCollisionExit(Collision collision)
    8.     {
    9.         --_collisions;
    10.     }
    11.  
    It checks if _collisions is over 0 to allow movement.

    Could also compare colliders to a cached collider in OnCollisionExit() to see if they are different.

    I think it could be improved with the option of having the events be ordered so that upon leaving an object's collider it will be followed up by OnCollisionEnter again to make sure it is always the last event called if true for at least one object. Using OnCollisionStay() is costly for performance and I would rather avoid using that.
     
    Last edited: Feb 15, 2020
  13. le44vraimentpartout

    le44vraimentpartout

    Joined:
    Oct 21, 2020
    Posts:
    1
    yah i have the same problem someone can tell me what is wrong in my scripts ?(im begginer)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [code=CSharp]using System.Collections;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8.  
    9. public class collision : MonoBehaviour
    10. {
    11.  
    12.     void OnCollisionEnter(Collision other)
    13.     {
    14.        if (other.gameObject.CompareTag("barrier"))
    15.         print("re");
    16.     }
    17. }
     
  14. megatron0

    megatron0

    Joined:
    Nov 19, 2018
    Posts:
    27
    first of all my case was that i imported an asset that added tags called collide and all objects haf to have so oncollison can be called so please check for this
    is this script sitting on the object that has the collider?
     
  15. mekarano51

    mekarano51

    Joined:
    Jan 24, 2021
    Posts:
    1
    I also had this problem, but I could find my emberassing error. I could not see the messages, because I deactivated the Unity messages in the console:oops:. Look if the buttons of the top-right corner are activated.
     
  16. Faour

    Faour

    Joined:
    Feb 15, 2021
    Posts:
    1
    im having the same exact issue here's my code

    private void OnCollisionEnter(Collision Collision);

    I literarily started like yesterday so I'm very very confused
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Please don't hijack old threads. It's against forum rules. Instead, create your own new thread... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/