拍摄动画开始后,如何通过脚本开始子弹拍摄?

时间:2019-04-18 00:10:10

标签: c# unity3d

在动画制作器的编辑器中,我处于动画“瞄准狙击步枪”的状态,该动画为循环射击提供动画。我不确定是否正确,但是我添加了一个新的参数名称Shooting type bool。并将其作为true添加到过渡中:

Animator and transition

在项目符号中,我添加了一个刚体和射击脚本:

Bullet

还有脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public GameObject projectile;

    private void Update()
    {
        GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
        bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10);
    }
}

我想做的是,一旦开始拍摄动画(瞄准狙击步枪),它将不停地开始发射子弹。

我以为我需要新的射击布尔参数,但不确定如何在脚本中使用它。

2 个答案:

答案 0 :(得分:2)

正如您所说,您可以在动画器中添加另一个参数,该参数在动画开始时设置为true,在动画结束时设置为false。要在代码中获取此参数,可以使用anim.GetBool("<name of the parameter>");,其中anim是指向主要播放器动画师的Animator变量。

您可以在此处了解更多信息:https://docs.unity3d.com/ScriptReference/Animator.GetBool.html

答案 1 :(得分:1)

这是一个可行的解决方案。

在过渡中的Animator的编辑器中,我正在使用参数射击,并在“条件”中将其设置为false:

Conditions

然后在“开始”脚本中将参数设置为true,然后在Update中检查其是否为真:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public GameObject projectile;
    public Animator anim;

    private void Start()
    {
        anim.SetBool("Shooting", true);
    }

    private void Update()
    {
        if (anim.GetBool("Shooting") == true)
        {
            GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
            bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10, ForceMode.VelocityChange);
        }
    }
}

拍摄动画开始后就开始拍摄。