Search Unity

How Do I Add Collision To My Object?

Discussion in '2D' started by Dogg, Apr 24, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Let me explain. You know how on the game called Flappy Bird when the bird touches the object, he dies? Well that's how I want it to be. When my character/player touches any of the objects that are spawning, I want him to die instantly along with a death animation. Since I'm making an infinite runner, he needs to die instantly. I already tried to do it on my own, but failed multiple time even though it's such a simple concept.

    You'll be seeing me a lot in the near future most likely, so sorry. Anyways here's the script I made it's incomplete so it doesn't look good, hope you guys can help me: http://pastebin.com/kL09W9DD
     
  2. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
    Would be simple really. First, make sure both the player and the objects have rigidbodies (make sure the objects don't have gravity enabled though, so they don't fall. and restrict there position and rotation). Then, once the collision happens disable the players controller script, and then change their animation to a death one. If they fall down, the rigidbody with gravity should just make him fall down without his controller affecting him.

    Code (csharp):
    1.  
    2. public class PipeHandler : MonoBehaviour {
    3.  
    4.         private bool _isDead = false;
    5.        
    6.         void Update() {
    7.                 Debug.Log(_isDead);
    8.         }
    9.        
    10.         OnCollisionEnter(Collision collision) {
    11.                 if (collision.gameObject.tag == "Player") {
    12.                         _isDead = true;
    13.                         collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false;
    14.                 }
    15.         }
    16. }
    17.  
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Thanks for the script, but it doesn't work sadly. Here's what I did. I fixed my script, then I added rigidbody2D to my individual objects in the prefab folder and disabled/enabled gravity which didn't do anything. It either stayed the same or started to float. I added my script and a rigidbody2D to my gameobject called Instantiate (it spawns the objects in the place I want it to) and my player which still didn't do anything. Then I tried to add it to my individual objects in the prefab folder, which only caused lag. How can something so simple be so hard for me?
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    There seems to be a lot of things that can break collisions. You don't need both objects to have rigidbodies, but at least one must have a non-kinematic rigidbody. The non-kinematic part seems to be left out of the tutorials and documentation and trips a lot of people up.
     
    junctionboss likes this.
  5. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    My rigidbody2D on my player was already non-kinematic. So that doesn't seem to be the problem. Could it possibly be the script that's the problem? Or maybe scripts attached to my player?
     
  6. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
    Perhaps a bit obvious though have you selected the 'Player' tag to the player object from the Inspector?
     
  7. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    First of all you guys are missing the "2D" in your method names....
    http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html

    Anyway, it should be as simple as this...
    1) Create a player with a rigid body and a collider
    2) Create an obstacle with a kinematic body and a collider
    3) Attach a script to the player, including the method OnCollisionEnter2D(). Upon collision do....
    a) Switch the graphic of the player to a dead image
    b) Disable the controls of the player
    c) Set the velocity of the player to zero
     
    nijatbabakhanov likes this.
  8. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    For choice B, you mean like this?: collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false; Or like this: collision.gameObject.SendMessage("Message");

    Oh and let me explain how my obstacles are. There are two obstacles in one prefab called object. I attach the object to a gameobject called Instantiate. I needed to do this in order to have them spawn at different places and to spawn infinitely. So should I attach a kinematic body to my individual obstacles, my object, or my Instantiate?
     
    Last edited: Apr 25, 2014
  9. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I already had my Player tagged as player. Do my obstacles need to be tagged as something?
     
  10. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Well is your OnCollisionEnter2D() method detecting any collisions at all?
     
  11. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    If you mean if my player collides with an obstacle, then yes. But if you mean does it appear on the console, then no. So no it does not seem to detect collisions at all in the console. Unless of course UnityEngine.Debug:Log(Object) means that it detects it. What should it say or do if my player collides with an obstacle?
     
  12. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    As a test try putting this in your script. It should output to the Console when a collision occurs...

    void OnCollisionEnter2D(Collision2D other) {
    print("OnCollisionEnter2D: " + other.gameObject.name);
    }
     
  13. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I did a test, it still didn't work. I tried messing with the Rigidbodies and the colliders again, but nothing seems to work. Maybe I put it in wrong. Is the script supposed to replace:

    OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.tag == "Player") {
    isDead = true;
    collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false;

    Or is it supposed to go with it like:

    void OnCollisionEnter2D(Collision2D other) {
    print("OnCollisionEnter2D: " + other.gameObject.name);
    _isDead = true;
     
  14. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    As a test the method I supplied should replace your method.
     
  15. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    It didn't work, and I had really high hopes for this one to. Maybe it has something to do with the 2D tools. I heard from some people that the 2D tools aren't working correctly in Unity. Is this true? If so should I try to use the 3D tools to make a 2D game?
     
  16. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    It should work. Many people including myself are successfully using the 2D tools even though there might be a bug here or there. I think you should analyze this sample project to see how they have everything setup with proper collision occurring...
    https://www.assetstore.unity3d.com/#/content/11228
     
  17. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay then. I'll analyze this game and see how their colliders and scripts are compared to mine, but it will have to wait. I've been trying to fix this problem since 3:00pm, so I'm tired. Thanks for the link.
     
  18. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Alright I analyzed the scripts and the game objects of the sample you gave me. Everything looks like mine except for a few added stuff like player health, taunts, etc. So now that I looked at that, I have put together a short script for the player death. When you have the time take a look at it to see if it will work or not. So far it doesn't so I know I'm missing something, so please check it out when you have the time. Thanks. http://pastebin.com/jYwhUSiG
     
  19. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Never mind it I finally got it to work. Thank you for all your help. Without you this would've taken longer. Thanks a lot!