void OnTriggerStay2D(Collider2D other) { RubyController controller = other.GetComponent <RubyController>(); if (controller != null) { controller.ChangeHP(-1); } }
void OnCollisionEnter2D(Collision2D other) { RubyController player = other.gameObject.GetComponent <RubyController>(); if (player != null) { player.ChangeHP(-1); } }
private void OnTriggerEnter2D(Collider2D collision) { RubyController rubyController = collision.gameObject.GetComponent <RubyController>(); if (rubyController != null) { rubyController.ChangeHP(-1); } Destroy(gameObject); }
private void OnCollisionEnter2D(Collision2D collision) { //注意collision如何调用getcomponent方法 RubyController rubyController = collision.gameObject.GetComponent <RubyController>(); if (rubyController != null) { rubyController.ChangeHP(-1); } }
private void OnTriggerStay2D(Collider2D collision) { //获取RubyController脚本,注意脚本是挂在角色上的,也就是这里的collision。 RubyController rubyController = collision.GetComponent <RubyController>(); Debug.Log(rubyController); //对rubyController获取进行判空处理,让代码更严谨 if (rubyController != null) { //调用血量改变参数 rubyController.ChangeHP(-1); Debug.Log(rubyController.CurrentHP); } }
private void OnTriggerEnter2D(Collider2D collision) { //获取RubyController脚本,注意脚本是挂在角色上的,也就是这里的collision。 RubyController rubyController = collision.GetComponent <RubyController>(); Debug.Log(rubyController); //对rubyController获取进行判空处理,让代码更严谨 if (rubyController != null) { //满血的时候是不能吃回血道具的 if (rubyController.CurrentHP < rubyController.maxHP) { //调用血量改变参数 rubyController.ChangeHP(1); rubyController.PlayAudio(audioClip); Instantiate(collectEffect, transform.position, Quaternion.identity); Destroy(gameObject); } } }