无法分配“死亡”,因为它是一个方法组

时间:2017-03-11 20:13:37

标签: c# artificial-intelligence unity5 ragdoll

我最近开始学习c#,但我遇到了一个错误,希望你们中的一些人能够帮助我。我们正在Unity5中开发游戏,当玩家健康命中0时,ra​​g-doll功能将启动但不幸的是我不能称为死亡,因为它是一个方法组...任何想法?干杯。这是代码(我评论了它,所以我可以在Unity中进入播放模式,因为错误不让我在第47和53行):

using UnityEngine;
using System.Collections;

public class EnemyAI_Basic : MonoBehaviour
{
    private EnemyAI_Basic enemyAI_Basic;
    Animator controller;
    float health;
    Animator anim;
    bool Alive = true;
    public bool Dead = false;
    int pointValue = 5;
    private Collider myCollider;
    private Rigidbody myRigidbody;

    CapsuleCollider capsuleCollider;


    void Start()
    {
        controller = GetComponentInParent<Animator>();
        health = 40;
        capsuleCollider = GetComponent<CapsuleCollider>();
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        if (!Dead)
        {
            anim.SetTrigger("Alive");
        }
    }

    void Death()
    {
        Dead = true;
        Alive = false;
        capsuleCollider.isTrigger = true;
        anim.SetTrigger("Dead");
        Destroy(gameObject, 4f);
    }

    void OnEnable()
    {
        SetInitialReferences();
        enemyAI_Basic.Death += ActivateRagdoll;
    }


    void OnDisable()
    {
        enemyAI_Basic.Death -= ActivateRagdoll;
    }

    void SetInitialReferences()
    {
        enemyAI_Basic = transform.root.GetComponent<EnemyAI_Basic>();

        if (GetComponent<Collider>() != null)
        {
            myCollider = GetComponent<Collider>();
        }

        if (GetComponent<Rigidbody>() != null)
        {
            myRigidbody = GetComponent<Rigidbody>();
        }

    }

    void ActivateRagdoll()
    {
        if (myRigidbody != null)
        {
            myRigidbody.isKinematic = false;
            myRigidbody.useGravity = true;
        }

        if (myCollider != null)
        {
            myCollider.isTrigger = false;
            myCollider.enabled = true;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我看到了你的问题。 在这种情况下,你就像代表一样对待死亡方法。您只能将方法注册到委托或事件,您不能将方法注册到方法组。 对于您的情况,有几种方法可以解决这个问题。

第一个解决方案,到目前为止最容易,在Death函数中调用ActivateRagdoll,如下所示:

void Death()
{
    Dead = true;
    Alive = false;
    capsuleCollider.isTrigger = true;
    anim.SetTrigger("Dead");
    ActivateRagdoll();
    Destroy(gameObject, 4f);
}

然后删除:

enemyAI_Basic.Death += ActivateRagdoll;

enemyAI_Basic.Death -= ActivateRagdoll;
来自OnEnable和OnDisable方法的

应该删除任何编译器错误。 现在,为了使用这种方法,您所要做的就是从代码中的某个地方调用它。根据你杀死Ai的方式,你可以考虑将该方法公之于众,或者在更新循环中有一些死亡逻辑,

if(health <= 0)
{
   Death();
   enabled = false;  //disable the component so the update loop doesn't repeat the death procedure multiple times, Unity might not be pleased.
}

另一种方式,以及您尝试这样做的方式是创建一个类可以订阅方法的事件。此事件将调用所有已订阅的方法,为他们提供通知的方式,无论何时发生有趣的事情,例如在这种情况下Ai死亡。这对于不应该经常检查Ai的死亡状态的外部类是有用的,毕竟,如果它完全健康,它们可能不关心Ai。他们只需要知道艾未未死的时候。事件是这种情况的理想选择。

要举办活动,你将会做这样的事情:

public class EnemyAI_Basic : MonoBehaviour
{
    //your other variables

    public delegate void EnemyDies();  //declare the method types that can be registered to the event

    public event EnemyDies onEnemyDeath; //declare event, using the delegate for the method type

    //other methods here

    private void OnEnable()
    {
        //must be the same method type as the event, the compiler will let you know if it isn't
        onEnemyDeath += Death;
        onEnemyDeath += ActivateRagdoll;            
    }

    private void OnDisable()
    {
        onEnemyDeath -= Death;
        onEnemyDeath -= ActivateRagdoll;
    }

    //other things here

    //doesn't have to be public, just in case you want a bullet to kill the enemy
    public void KillAi()
    {
        //checking for null basically says: is anybody registered? If not, nothing to invoke. 
        //It will get upset if you try and invoke an event without any registered methods, hence the check.
        if(onEnemyDeath != null)
        {
            onEnemyDeath();
        }
    }

    void Death()
    {
       Dead = true;
       Alive = false;
       capsuleCollider.isTrigger = true;
       anim.SetTrigger("Dead");
       //ActivateRagdoll();   You don't have to manually call this now, invoking the event will call it for you.
       //Registration order is important, me thinks. Experiment :)
       Destroy(gameObject, 4f);
    }
}

还有其他几种方法来装配这样的系统,但是,在你的情况下,这可能是你所追求的。

一些阅读:

活动:https://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable https://www.tutorialspoint.com/csharp/csharp_events.htm

代表:https://msdn.microsoft.com/en-us/library/ms173171.aspx

方法组:What is a method group in C#?

希望这有帮助。