【问题标题】:How can I enable shooting only when the npc is in aiming animation?仅当 npc 处于瞄准动画时,如何才能启用射击?
【发布时间】:2019-09-10 05:41:51
【问题描述】:

我有一个动画师。首先NPC开始走路,然后他慢慢停下来瞄准。然后用一个脚本,当我单击鼠标左键时,它会射击。这是 Animator 中的最后一个拍摄状态。 在行走状态和瞄准状态之间,我添加了一个参数,并在过渡中将其设置为 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处于瞄准动画中时才能启用射击。

【问题讨论】:

  • 你可以用Play("AIMING") 代替Play("SHOOTING") 并从瞄准过渡到射击?
  • 您可以将两个事件添加到动画时间轴。第一个在启用瞄准时触发,并调用一个方法并设置一个布尔变量,另一个在禁用瞄准时触发并调用另一个方法并重置相同的变量。
  • 我将添加我自己的工作解决方案,因为它工作得非常好,就像我想要的那样。

标签: c# unity3d


【解决方案1】:
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 方法并在更新中使用它,它现在工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2015-09-10
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多