97 lines
2.4 KiB
C#
97 lines
2.4 KiB
C#
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;
|
|
void Start()
|
|
{
|
|
rig = GetComponent<Rigidbody2D>();
|
|
anim = GetComponent<Animator>();
|
|
player = FindObjectOfType<Player>(); ;
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|