Question by 
               unity_-1ergJkVyfIXKg · Nov 13, 2018 at 03:57 PM · 
                triggercollider2dtriggers2d platformer  
              
 
              2D platformer Hide Action not working
Hello, Ive got problem with my script. Im trying to make my player Hide from enemy on trigger only, Script that i made (Im ultra begginer) works only sometimes (about 2 times for 10 attempts). Why? Heres my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
 public int health = 100;
 bool hide = false;
 public GameObject zly;
 public Collider2D objectCollider;
 public Collider2D anotherCollider;
 void Start()
 {
     objectCollider = GameObject.FindGameObjectWithTag("Player").GetComponent<BoxCollider2D>();
     anotherCollider = GameObject.FindGameObjectWithTag("Shelter").GetComponent<BoxCollider2D>();
 }
 public void TakeDamage(int damage)
 {
     health -= damage;
     if (health <= 0)
     {
         Die();
     }
 }
 public void Update()
 {
     if (objectCollider.IsTouching(anotherCollider))
     {
         if (Input.GetButtonDown("Hide"))
         {
             hide = true;
             Physics2D.IgnoreCollision(zly.GetComponent<BoxCollider2D>(), this.GetComponent<BoxCollider2D>(), true);
             Physics2D.IgnoreCollision(zly.GetComponent<BoxCollider2D>(), this.GetComponent<CircleCollider2D>(), true);
             Debug.Log("Hide", gameObject);
         }
     }
     if (Input.GetButtonUp("Hide"))
     {
         hide = false;
         Physics2D.IgnoreCollision(zly.GetComponent<BoxCollider2D>(), this.GetComponent<BoxCollider2D>(), false);
         Physics2D.IgnoreCollision(zly.GetComponent<BoxCollider2D>(), this.GetComponent<CircleCollider2D>(), false);
     }
 }
         
 void OnCollisionEnter2D (Collision2D col)
 {
     if (col.gameObject.name == "Enemy")
     {
         if (hide == false)
         {
             Die();
         }
         else if (hide == true)
         {
             
         }
     }
 }
 void Die()
 {
     Destroy(gameObject);
 }
}
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Pipe Game ... I have to trace water from source to destination 2 Answers
Trigger events with bullets 0 Answers
Trigger the Animation #2 after Animation #1 was triggered 0 Answers
My character seems to glitch out after touching trigger colliders. 0 Answers
How to guarantee that only one trigger is activated at the same time? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                