1.添加animation的编辑窗口
2.选中animation编辑窗口,点击角色,选择要添加动画帧时机的动画(Animator中的动作),如Shooting。
3.选择出招的关键帧(1),添加事件(2)
4.点击关键帧的事件,选择要执行的代码函数方法(前提:该代码已挂在角色身上),完成!
5.射箭的函数方法
6.同上,在攻击结束的最后一帧加上关键帧,然后将StopShoot(停止攻击函数方法)载入,即可完成攻击间隔冷却
7.animator如图,关于shooting的箭头分别用bool值shooting来判断
8.PlayerShoot.cs实现代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public Animator anim;
public GameObject arrowPrefab;
bool shootPressed;
Vector3 arrowPos = new Vector3(0.11f, -0.38f, 0f);//箭矢发射的位置
Quaternion arrowRotation = Quaternion.identity;//箭矢朝向
public float intervalTime;//攻击间隔
public float startTime;//箭矢发射时间
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
//anim = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>();//触发器获取动画?
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.J))
{
shootPressed = true;
}
}
private void FixedUpdate()
{
Shoot();
}
void Shoot()
{
if(shootPressed == true)
{
anim.SetBool("Shooting",true);
//anim.SetBool("Shooting",false);
//StartCoroutine(StartShoot());
shootPressed = false;
/*float faceDirection = transform.localScale.x;//角色朝向
if (faceDirection > 0)//角色朝左
{
arrowRotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
}
else//角色朝右
{
arrowRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
}*/
}
}
void StartShoot()
{
float faceDirection = transform.localScale.x;//角色朝向
if (faceDirection > 0)//角色朝左
{
arrowRotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
}
else//角色朝右
{
arrowRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
}
}
void StopShoot()
{
anim.SetBool("Shooting", false);
}
}