【问题标题】:Unity 5 checking for animationUnity 5检查动画
【发布时间】:2017-04-16 23:27:28
【问题描述】:

我有一个玩家对象和一个敌人对象,它们的身体和武器上都有碰撞器,但这会导致它们在彼此碰撞时造成伤害。我想要的是这样他们只有在“isAttacking”动画运行时才会造成伤害。我的代码附加到玩家和敌人对象上,如下所示:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class detectHit : MonoBehaviour
{

public Slider healthbar;
Animator anim;
public string opponent;
public Collider ecollider;

// Use this for initialization
void Start()
{
    anim = GetComponent<Animator>();
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag != opponent) return;

        healthbar.value -= 20;

    if (healthbar.value <= 0)
    {
        anim.SetBool("isDead", true);
        ecollider.gameObject.SetActive(false);
    }

}

// Update is called once per frame
void Update()
{

}
}

我试过上网,但大多数东西都过时了,我觉得我需要做这样的事情:

if(anim.GetCurrentAnimatorStateInfo(0).IsName("isAttacking")){
healthbar.value -= 20;
}

但这不起作用,也许是因为我没有看到对手的动画?如果是这种情况,我只是不知道如何寻找敌人的动画。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# unity5


    【解决方案1】:

    为 child 中的角色添加攻击对象和对撞机。默认情况下关闭此对撞机的活动。 如果角色开始攻击动画,在第一帧打开这个碰撞器。并在最后一帧关闭。 你可以捕捉攻击动作。

    【讨论】:

      【解决方案2】:

      首先,如果您想检查其他脚本中的某些内容,则需要对其进行引用。您可以将敌人 GameObject/Prefab 拖到您存储公共属性的插槽中。您只需要有一个 GameObject 类型的公共字段,或者您甚至可以将类型设置为 Script。

      public GameObject myEnemyReference;
      

      当场景的层次结构中没有敌人时,当您通过脚本实例化敌人来启动和创建敌人时,您还必须存储一个引用。

      GameObject toInstantiate = myEnemyPrefab;
      GameObject myEnemyReference = Instantiate (toInstantiate, new Vector3 (0f, 0f, 0f), Quaternion.identity) as GameObject;
      

      获得参考后,您可以像这样调用脚本的公共函数:

      detectHit enemyScript = (detectHit) myEnemyReference.GetComponent (typeof(detectHit));
      bool isAttackState = enemyScript.checkForHitable ();
      

      这里我只是假设脚本有一个名为 checkForHitable 的公共函数,当允许对手受到伤害时,它返回 true。

      我不明白你希望你的战斗系统如何工作,但由于你的代码不能正常工作,你可能想检查玩家和敌人的动画状态,这里有一个建议你可以怎么做检查敌人状态甚至进一步调用敌人脚本的函数,这可能对以后有帮助。

      PS:类名通常以大写字母开头:DetectHit ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 2021-06-04
        • 2022-01-15
        • 2020-09-23
        相关资源
        最近更新 更多