简体   繁体   English

统一3D脚本

[英]Scripting in unity 3d

I am facing a problem in Unity3D. 我在Unity3D中遇到问题。 I have the same health script attached to both the player and the enemy. 我拥有与玩家和敌人相同的健康脚本。 I want to show the game-over message when the player dies but the issue is that the game over message appears for both the player and enemy when they die. 我想在玩家死亡时显示游戏结束消息,但问题是玩家和敌人死亡时都会出现游戏结束消息。

My code looks like is that: 我的代码如下所示:

public class CharacterStats : MonoBehaviour 
{
    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update () 
    {
        health = Mathf.Clamp (health, 0, 100);
    }

    public void damage(float damage)
    {
        health -= damage;
        if(health<=0)
        {
            Die();
            Application.LoadLevel(gameover);
        }
    }

    void Die()
    {
        characterController.enabled = false;
        if (scriptstodisable.Length == 0)
            return;
                 foreach (MonoBehaviour scripts in scriptstodisable)

                scripts.enabled = false;
                 if (ragdollmanger != null)
                     ragdollmanger.Raggdoll();
    }
}

As you are using 1 script for both player and enemy. 当您同时为玩家和敌人使用1个脚本时。 You should have different classes for both and implement an interface or derive from a base class to implement health: 您应该为这两者使用不同的类,并实现一个接口或从基类派生以实现运行状况:

public class Character : MonoBehaviour 
{
    public float Health;

    public virtual void Damage(float damageValue) 
    {
        Health -= damageValue;
    }

    public virtual void Die()
    {

    }
}

public Enemy : Character
{
    public override void Die()
    {
        // do enemy related stuff
    }
}

public Player : Character
{   
    public override void Die()
    {
        // do player related stuff.
        // like game over screen
    }
}

Hope this helps :) 希望这可以帮助 :)

You could use a bool to check whether CharacterStats is attached to the player, for example by adding a tag called ”Player” to the player game object and checking if gameObject.tag == “Player” , or you could equivalently name the game object ”Player” and check gameObject.name if you so wish. 您可以使用bool来检查CharacterStats是否已附加到玩家,例如,通过在玩家游戏对象上添加一个名为”Player”的标签并检查gameObject.tag == “Player” ,或者可以等效地命名游戏对象如果需要,请”Player”并检查gameObject.name

You could then run the function for the game over message only if the game object is a player ( isPlayer is true). 然后,仅当游戏对象是玩家( isPlayer为true)时,才可以运行游戏结束消息功能。

public class CharacterStats : MonoBehaviour 
{
    bool isPlayer = false;

    // Use this for initialization
    void Start () 
    {
        if(gameObject.tag == “Player”) 
        {
            isPlayer = true;
        }      
    }

    // Update is called once per frame
    void Update () 
    {
        health = Mathf.Clamp (health, 0, 100);
    }

    public void damage(float damage)
    {
        health -= damage;
        if(health<=0)
        {
            if(isPlayer)
            {
                // Do Player-only stuff
            }

            // Do Stuff for both players and enemies
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM