Search Unity

Player not dead when collides with obstacle

Discussion in '2D' started by motionlife89, Oct 30, 2014.

  1. motionlife89

    motionlife89

    Joined:
    Dec 23, 2013
    Posts:
    19
    Good day!

    I am making a flappy bird inspired game for my school project by following this tutorial:

    anwell.me/articles/unity3d-flappy-bird/

    However, when I tried to preview the game, the player isn't dying when it collides with the obstacle on top as seen in the screenshot attached yet keeps moving up and down and only dies when it hits the obstacle at the bottom.

    Why is the happening and what did I do wrong?





    Also, I am attaching additional screenshots for reference.



    c# code for runner:

    Code (csharp):
    1. using System.Collections;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4. public class runner : MonoBehaviour {
    5.    
    6.     public Vector2 jumpForce = new Vector2(0, 1);
    7.  
    8.     public AudioClip[] blips;
    9.    
    10.     void Update () {
    11.        
    12.         if (Input.GetKeyUp("space")) {        
    13.             rigidbody2D.velocity = Vector2.zero;          
    14.             rigidbody2D.AddForce(jumpForce);          
    15.         }
    16.        
    17.         Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
    18.        
    19.         if (screenPosition.y > Screen.height || screenPosition.y < 0)    
    20.             Die();
    21.     }
    22.    
    23.  
    24.     void OnCollisionEnter2D (Collision2D other) {
    25.        
    26.         if (other.gameObject.tag == "food")
    27.         {
    28.             Debug.Log("Collided food!");
    29.  
    30.             audio.PlayOneShot(blips[0]); //part that changed
    31.             Destroy (other.gameObject);
    32.         }
    33.  
    34.    
    35.         else
    36.             Die();
    37.     }
    38.  
    39.    
    40.     void Die () {
    41.         Destroy(this.gameObject);
    42.         Application.LoadLevel(Application.loadedLevel);
    43.     }


    Obstacle code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class obstacle : MonoBehaviour {
    5.  
    6.     public Vector2 velocity = new Vector2(-4, 0);
    7.    
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         rigidbody2D.velocity = velocity;
    12.     }
    13. }


    and the generate obstacle code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class generate : MonoBehaviour {
    5.  
    6.     public GameObject rocks;
    7.    
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         InvokeRepeating("CreateObstacle", 1f, 1.5f);
    12.     }
    13.    
    14.     void CreateObstacle()
    15.     {
    16.         Instantiate(rocks);
    17.     }
    18. }

    Any response is appreciated.
     
  2. Samdeman22

    Samdeman22

    Joined:
    May 19, 2014
    Posts:
    4
    Do you have a collider 2D for that obstacle? If so, go to that collider and tick isTrigger, this will make the obstacle send out a collision event instead of blocking other collider2Ds. Then make sure that your player also has a collider 2d with the appropriate shape, but with isTrigger deselected.