using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tiger : MonoBehaviour { Animator anim; Player player; Rigidbody2D rig; public float dis; public float speed; public Vector2 bcakJumpVel; public int dieCount; SpriteRenderer[] childrenSp ; public Color oriColor; public Color hurtColor; int count = 0; void Start() { childrenSp = GetComponentsInChildren(); rig = GetComponent(); anim = GetComponent(); player = FindObjectOfType(); ; } // Update is called once per frame void Update() { if (anim.GetBool("老虎走动")) if (Mathf.Abs(player.transform.position.x - transform.position.x) < dis) { anim.SetTrigger("老虎前扑"); anim.SetTrigger("老虎冲撞"); anim.SetBool("老虎走动", false); } } private void FixedUpdate() { if (anim.GetBool("老虎走动")) { rig.velocity = new Vector2(speed, rig.velocity.y); } //else rig.velocity = Vector2.zero; } public void Behavior(string behaviorName) { try { //触发动画 anim.SetTrigger(behaviorName); } catch (System.Exception e) { e.ToString(); Debug.LogError("没有找到触发器" + behaviorName); } } public void BackJump() { rig.velocity = bcakJumpVel; ; } public void JudgeForward() { if (Mathf.Abs(player.transform.position.x - transform.position.x) >= dis && !anim.GetBool("老虎走动")) { anim.SetBool("老虎走动", true); } else if (Mathf.Abs(player.transform.position.x - transform.position.x) < dis) { anim.SetTrigger("老虎冲撞"); anim.SetTrigger("老虎前扑"); anim.SetBool("老虎走动", false); } } //重置所有trigger public void ResetAllTrigger() { rig.velocity = Vector2.zero; AnimatorControllerParameter[] aps = anim.parameters; for (int i = 0; i < aps.Length; i++) { AnimatorControllerParameter paramItem = aps[i]; if (paramItem.type == AnimatorControllerParameterType.Trigger) { string triggerName = paramItem.name; bool isActive = anim.GetBool(triggerName); if (isActive) { anim.ResetTrigger(triggerName); } } } } public void Freeze() { rig.velocity = Vector2.zero; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.TryGetComponent(out Item weapon)) { Debug.Log(weapon.name + "" + weapon.GetIsAtk()+"打到老虎"); weapon.SetIsAtk(false); count++; ChangeColor(); Invoke("ResetColor",0.5f); if (count >= dieCount) { ResetAllTrigger(); Freeze(); anim.SetTrigger("老虎似了"); GetComponent().enabled = false; Debug.Log("老虎似了"); } } } void ChangeColor() { foreach (SpriteRenderer sp in childrenSp) { sp.color = hurtColor; } } void ResetColor() { foreach (SpriteRenderer sp in childrenSp) { sp.color = oriColor; } } }