【问题标题】:How to prevent the gameObjects being destroyed with the same script on Collision?如何防止在碰撞中使用相同的脚本破坏游戏对象?
【发布时间】:2016-10-20 07:59:29
【问题描述】:

当玩家对其中一个游戏对象进行攻击时,所有游戏对象(敌人)都会被摧毁。如果它与当前的游戏对象(敌人)发生碰撞,我希望它破坏游戏对象(敌人),而其他游戏对象不会被破坏。所有的敌人都有相同的脚本。


这是附加在 MainCharacter 游戏对象中的运动脚本。

public class Movement : MonoBehaviour
{
    public float movementSpeed = 6.0f;
    public GameObject player;
    public int jumpHeight = 350;
    private bool onGround = false;
    private bool afterMovingPlatform = false;
    Animator anim;

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

    void Update()
    {
        //these are the codes for keyboard inputs when walking, attacking, etc...
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Ground")
        {
            onGround = true;

            if (afterMovingPlatform)
            {
                transform.parent = null;
                afterMovingPlatform = false;
            }
        }

        if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left")))
        {
            EnemyHealth.giveDamage();
        }
        else if (coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy")
        {
            CoinScore.score = 0;
            Health.health = 3;
            EnemyHealth.enemyHealth = 1;
            SceneManager.LoadScene("LevelF");
            Debug.Log("RESPAWN");
        }

        if (coll.gameObject.tag == "MovingPlatform")
        {
            onGround = true;
            afterMovingPlatform = true;
            transform.parent = coll.transform;
        }   
    }

    //void onCollisionExit2D(Collision2D coll)
    //{
    //    Debug.Log("EXIT");
    //    if (coll.gameObject.tag == "MovingPlatform")
    //    {
    //        transform.parent = null;
    //    }
    //}
}


附在敌人身上的脚本。他们都有 Enemy Healths: 1 统一输入。

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {

    public static int enemyHealth = 1;
    public Transform explosion;
    public int enemyHealths;
    CircleCollider2D coll;

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

    // Update is called once per frame
    void Update()
    {
        enemyHealths = enemyHealth;
        if (enemyHealths <= 0)
        {
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(coll.gameObject);
        }
    }

    public static void giveDamage()
    {
        enemyHealth -= 1;
    }
}

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    仅仅是因为函数 giveDamage 被声明为 static(enemyHealth 成员也是如此)

    使用 static 修饰符声明一个静态成员,它属于类型本身而不是特定对象。

    因此,每个敌人共享相同的生命值,当你伤害一个时,实际上你伤害了所有敌人。

    每个敌人都必须有自己的生命值。删除 static 关键字并在您的播放器脚本中执行以下操作

    if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left")))
    {
        coll.gameObject.GetComponent<EnemyHealth>().giveDamage();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2018-06-23
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多