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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collision help needed

Discussion in 'Scripting' started by xaviergamerpro9, Jul 11, 2020.

  1. xaviergamerpro9

    xaviergamerpro9

    Joined:
    Jul 10, 2020
    Posts:
    12
    Hello, I am currently working on a game where if you collide with objects you earn points or die depending on the object. Though when you collide with an object you go right through.

    Please help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Use trigger colliders for your obstacles. You can process earning points or dying in OnTriggerEnter().
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    If you want the object to go through another object on collision, use triggers as @PraetorBlue said. Triggers are basically collisions without physics involved. So you get notified, but objects dont care about the collision otherwise. You would use something like that for collecting items.
    However, if you do not want to go through the (all) object as implied by your last statement, use normal colliders instead. This is useful for items you can collide with and want to stop again, for example to reset velocity, take damage and potentially kill the player on contact. Even tho you could also simulate all this with triggers (for example by manually resetting the player velocity on trigger enter), so it mostly depends on whether you want physics to be involved or not.

    It is also important to note that collisions of both kinds will only work if the colliding object has a rigidbody component, and that two triggers cannot collide.
    More information can be found in the documentation where this is also explained.
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
    https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
     
  4. xaviergamerpro9

    xaviergamerpro9

    Joined:
    Jul 10, 2020
    Posts:
    12
    Thank you, I re-did my collision and now it works.

    The script just detected collision wrong (I used OnCollisionEnter when its a 2d game but I switched to OnCollisionEnter2D )