如何在npc瞄准动画时启用拍摄?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在npc瞄准动画时启用拍摄?相关的知识,希望对你有一定的参考价值。
我有一个动画师。首先,npc开始行走,然后他慢慢停下来瞄准。然后用脚本我做了,当我点击鼠标左键时它会拍摄。这是Animator中的最后一个拍摄状态。在walk状态和瞄准状态之间,我添加了一个参数并在转换中将其设置为true:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
[SerializeField]
private Transform[] firePoints;
[SerializeField]
private Rigidbody projectilePrefab;
[SerializeField]
private float launchForce = 700f;
[SerializeField]
private Animator anim;
[SerializeField]
private bool automaticFire = false;
[SerializeField]
private bool slowDownEffect = false;
private void Start()
{
anim.SetBool("Shooting", true);
}
public void Update()
{
if (Input.GetButtonDown("Fire1") && automaticFire == false)
{
if (anim.GetBool("Shooting") == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
else
{
if (automaticFire == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
}
private void LaunchProjectile()
{
foreach (var firePoint in firePoints)
{
Rigidbody projectileInstance = Instantiate(
projectilePrefab,
firePoint.position,
firePoint.rotation);
projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
}
}
}
问题是,当我运行游戏时,如果我点击鼠标左键“Fire1”,它将立即从步行动画跳转/改变为拍摄动画并开始拍摄。
我希望首先npc会走路然后它会改变为瞄准当他完成改变瞄准然后才能射击“Fire1”。
如果npc仍在行走或中间改变瞄准不允许射击。只有在npc处于瞄准动画时才能启用拍摄。
答案
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
[SerializeField]
private Transform[] firePoints;
[SerializeField]
private Rigidbody projectilePrefab;
[SerializeField]
private float launchForce = 700f;
[SerializeField]
private Animator anim;
[SerializeField]
private bool automaticFire = false;
[SerializeField]
private bool slowDownEffect = false;
private void Start()
{
anim.SetBool("Shooting", true);
}
public void Update()
{
if (isAnimationStatePlaying(anim, 0, "AIMING") == true)
{
if (Input.GetButtonDown("Fire1") && automaticFire == false)
{
if (anim.GetBool("Shooting") == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
else
{
if (automaticFire == true)
{
anim.Play("SHOOTING");
LaunchProjectile();
}
}
}
}
private void LaunchProjectile()
{
foreach (var firePoint in firePoints)
{
Rigidbody projectileInstance = Instantiate(
projectilePrefab,
firePoint.position,
firePoint.rotation);
projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
}
}
bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
return true;
else
return false;
}
}
我添加了isAnimationStatePlaying方法并在Update中使用它,它现在工作得非常好。
以上是关于如何在npc瞄准动画时启用拍摄?的主要内容,如果未能解决你的问题,请参考以下文章