Search Unity

Trying to add player Invincibility whenever It touches lava, but it doesn't recognize the collision

Discussion in '2D' started by DiamondEyes, May 23, 2018.

  1. DiamondEyes

    DiamondEyes

    Joined:
    May 20, 2018
    Posts:
    3
    It will walk on the surface just fine, but it will never take any damage.

    Top is the section of code I'm working with, bottom is the full player script
    Code (CSharp):
    1.  
    2.      void OnCollisionEnter2D(Collision2D coll)
    3.     {
    4.         if (!invincible)
    5.         {
    6.             if (coll.gameObject.name == "Lava")
    7.             {
    8.              
    9.                 StartCoroutine(Hurt());
    10.             }
    11.         }
    12.     }
    13.  
    14.  
    15.     private IEnumerator Hurt()
    16.     {
    17.         healthPoints--;
    18.         animator.SetTrigger("PlayerHurt");
    19.         invincible = true;
    20.         HealthText.text = "HP: " + healthPoints;
    21.         yield return new WaitForSeconds(3);
    22.         invincible = false;
    23.        
    24.     }
    25. [code=CSharp]using System.Collections;
    26. using System.Collections.Generic;
    27. using UnityEngine;
    28. using UnityEngine.UI;    //Allows us to use UI.
    29. using UnityEngine.SceneManagement;
    30. public enum Direction { LEFT, RIGHT };
    31. public class PlayerPlatformerController : PhysicsObject {
    32.  
    33.     public bool invincible = false;
    34.  
    35.     public int score = 0;
    36.  
    37.     private int ore = 50;
    38.     private int chip = 25;
    39.     private int EPup = 5;
    40.     private int HPup = 2;
    41.  
    42.     public float maxSpeed = 3f;
    43.     public float jumpTakeOffSpeed = 5f;
    44.     public float flyingPower = 2.25f;
    45.  
    46.     public int slashPower = 3;
    47.     public int shootPower = 1;
    48.  
    49.     public Text ScoreText;
    50.     public Text HealthText;
    51.     public Text EnergyText;
    52.  
    53.     public bool isShooting = false;
    54.     public bool isSlashing = false;
    55.     public bool isFlying = false;
    56.  
    57.     public int energyPoints = 20;
    58.     public int healthPoints = 10;
    59.  
    60.     private SpriteRenderer spriteRenderer;
    61.     private Animator animator;
    62.     private Direction playerDirection = Direction.RIGHT;
    63.  
    64.     //private Transform _transform;
    65.     public GameObject Energy;
    66.  
    67.  
    68.     protected void Start()
    69.     {
    70.         spriteRenderer = GetComponent<SpriteRenderer> ();
    71.         animator = GetComponent<Animator> ();
    72.         //_transform = GetComponent(typeof(Transform)) as Transform;
    73.  
    74.         //  energy = GameManager.instance.energyPoints;
    75.         ScoreText.text = "Score: " + score;
    76.         EnergyText.text = "Energy: " + energyPoints;
    77.         HealthText.text = "HP: " + healthPoints;
    78.     }
    79.  
    80.     private void OnDisable()
    81.     {
    82.        // GameManager.instance.energyPoints = energy;
    83.     }
    84.  
    85.  
    86.     private void OnTriggerEnter2D(Collider2D other)
    87.     {
    88.         if (other.tag == "Exit")
    89.         {
    90.             //put stuff
    91.         }
    92.  
    93.         else if (other.tag == "Chip")
    94.         {
    95.             score += chip;
    96.             ScoreText.text = "Score: " + score;
    97.         }
    98.  
    99.         else if (other.tag == "Ore")
    100.         {
    101.             score += ore;
    102.             ScoreText.text = "Score: " + score;
    103.         }
    104.         else if (other.tag == "EPup")
    105.         {
    106.             energyPoints += EPup;
    107.             EnergyText.text = "Energy: " + energyPoints;
    108.         }
    109.         else if (other.tag == "HPup")
    110.         {
    111.             healthPoints += HPup;
    112.             HealthText.text = "HP: " + healthPoints;
    113.         }
    114.  
    115.  
    116.     }
    117.    
    118.    
    119.  
    120.  
    121.    
    122.      void OnCollisionEnter2D(Collision2D coll)
    123.     {
    124.         if (!invincible)
    125.         {
    126.             if (coll.gameObject.name == "Lava")
    127.             {
    128.              
    129.                 StartCoroutine(Hurt());
    130.             }
    131.         }
    132.     }
    133.  
    134.  
    135.     private IEnumerator Hurt()
    136.     {
    137.         healthPoints--;
    138.         animator.SetTrigger("PlayerHurt");
    139.         invincible = true;
    140.         HealthText.text = "HP: " + healthPoints;
    141.         yield return new WaitForSeconds(3);
    142.         invincible = false;
    143.        
    144.     }
    145.  
    146.  
    147.     public Direction PlayerDirection
    148.     {
    149.  
    150.         get
    151.         {
    152.             return playerDirection;
    153.         }
    154.     }
    155.  
    156.     void Update()
    157.     {
    158.  
    159.         AttackSlash();
    160.  
    161.         ComputeVelocity();
    162.  
    163.         AttackEnergy();
    164.        
    165.     }
    166.  
    167.  
    168.  
    169.  
    170.     void AttackSlash()
    171.     {
    172.         if (Input.GetKeyDown(KeyCode.Z))
    173.         {
    174.  
    175.             animator.SetTrigger("PlayerSlash");
    176.  
    177.         }
    178.  
    179.  
    180.     }
    181.  
    182.  
    183.  
    184.     void AttackEnergy()
    185.     {
    186.      
    187.        
    188.            
    189.  
    190.         if (Input.GetKeyDown(KeyCode.X) && energyPoints > 0 )
    191.         {
    192.          
    193.  
    194.             var tEnergy = Instantiate(Energy, gameObject.transform.position, Energy.transform.rotation) as GameObject;
    195.             tEnergy.GetComponent<Energy>().energyDirection = PlayerDirection;
    196.             animator.SetTrigger("PlayerEnergy");
    197.  
    198.             energyPoints--;
    199.  
    200.             EnergyText.text = "Energy: " + energyPoints;
    201.  
    202.             isShooting = true;
    203.  
    204.             if (!Input.GetKeyUp(KeyCode.X))
    205.             {
    206.                 isShooting = true;
    207.             }
    208.  
    209.         }
    210.  
    211.        
    212.  
    213.         if (Input.GetKeyUp(KeyCode.X))
    214.         {
    215.            
    216.             isShooting = false;
    217.  
    218.         }
    219.  
    220.         if (energyPoints < 0)
    221.         {
    222.             energyPoints = 0;
    223.         }
    224.         if (energyPoints > 20)
    225.         {
    226.             energyPoints = 20;
    227.         }
    228.  
    229.     }
    230.  
    231.  
    232.  
    233.  
    234.     protected override void ComputeVelocity()
    235.     {
    236.  
    237.      
    238.         Vector2 move = Vector2.zero;
    239.  
    240.         move.x = Input.GetAxis ("Horizontal");
    241.  
    242.     if (move.x > 0.01f)
    243.     {
    244.         playerDirection = Direction.RIGHT;
    245.  
    246.     }
    247.     else if (move.x < -0.01f)
    248.     {
    249.         playerDirection = Direction.LEFT;
    250.  
    251.     }
    252.  
    253.  
    254.  
    255.     if (Input.GetButtonDown ("Jump") && grounded) {
    256.             velocity.y = jumpTakeOffSpeed;
    257.             animator.SetTrigger("PlayerFlap");
    258.             isFlying = true;
    259.             animator.SetTrigger("PlayerFly");
    260.  
    261.  
    262.         } else if (Input.GetButtonDown ("Jump"))
    263.         {
    264.             animator.SetTrigger("PlayerFlap");
    265.             velocity.y = flyingPower;
    266.             isFlying = true;
    267.             animator.SetTrigger("PlayerFly");
    268.         }
    269.  
    270.         if (move.x > 0.01f)
    271.         {
    272.             animator.SetTrigger("PlayerMove");
    273.             if (spriteRenderer.flipX == true)
    274.             {
    275.                 spriteRenderer.flipX = false;
    276.             }
    277.         }
    278.         else if (move.x < -0.01f)
    279.         {
    280.             animator.SetTrigger("PlayerMove");
    281.             if (spriteRenderer.flipX == false)
    282.             {
    283.                 spriteRenderer.flipX = true;
    284.             }
    285.         }
    286.  
    287.         animator.SetBool ("grounded", grounded);
    288.         animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);
    289.        // animator.SetBool("flying", isFlying);
    290.  
    291.         targetVelocity = move * maxSpeed;
    292.     }
    293. }
    [/code]
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Does your player have a RigidBody2d and Collider2D? Did you check the Physics2D project settings, specifically the collision matrix to make sure the two gameobjects are interacting? Place a Debug.Log statement in your OnCollisionEnter2D to see if it ever enters that method.

    Also, it is probably a little better practice to use tags when comparing your gameobject in your OnCollisionEnter2D instead of the name directly. It is what they are intended for.
    if (coll.CompareTag("tagname"))