OnCollisionEnter is not called in unity

前端 未结 6 1859
走了就别回头了
走了就别回头了 2021-01-04 06:10

I checked nearly every answer for this, but those were mostly simple errors and mistakes. My problem is that OnCollisionEnter is not called even when colliding whith other r

相关标签:
6条回答
  • 2021-01-04 06:42

    You just need attach script to the same object, whose need detects the collision.

    0 讨论(0)
  • 2021-01-04 06:48

    because you misstyped class name of parameter. this makes no error also not works. eg:

    OnCollisionEnter(Collider other) //this is wrong
    OnCollisionEnter(Collision other) //this is correct
    
    0 讨论(0)
  • 2021-01-04 06:49

    Try this

    http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

    using UnityEngine;
    using System.Collections;
    
    public class Example : MonoBehaviour {
      void OnCollisionEnter(Collision collision) {
    
        foreach (ContactPoint contact in collision.contacts) {
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
    
        if (collision.relativeVelocity.magnitude > 2){
            audio.Play();        
        }
    
      }
    }
    
    0 讨论(0)
  • 2021-01-04 06:51

    You need to make sure that the collision matrix (Edit->Project Settings->Physics) does not exclude collisions between the layers that your objects belong to.

    Unity Docs

    You also need to make sure that the other object has : collider, rigidbody and that the object itself or either of these components are not disabled.

    0 讨论(0)
  • 2021-01-04 06:58

    Here is what I do:

    1. Make sure that object you wish to collide with target has non-kinematic rigidbody and mesh collider. My hitter object is a cube and just change its collider to mesh collider
    2. On mesh colider inspector make sure you enable convex. Please see more mesh collider inspector detail here

    Now your OnCollisionEnter works. I hope this helps you.

    0 讨论(0)
  • 2021-01-04 06:59

    Are you using 2D colliders and rigidbodies? If so use this function instead of OnCollisionEnter

    void OnCollisionEnter2D(Collision2D coll)
        {
            Debug.Log(coll.gameObject.tag);
    
        }
    
    0 讨论(0)
提交回复
热议问题