首页 文章

如何在Collision上使用相同的脚本防止游戏对象被破坏?

提问于
浏览
0

当玩家按下其中一个游戏对象时,所有游戏对象(敌人)都会被摧毁 . 我想让它破坏gameObject(敌人),如果它与当前的碰撞,而其他人不会被摧毁 . 所有的敌人都有相同的脚本 .

这是运动脚本,它附加在MainCharacter gameObject中 .

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;
    }
}

1 回答

  • 2

    只是因为函数 giveDamage 被声明为 static (敌人 Health 成员也是如此)

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

    因此,每个敌人拥有相同的 Health 数量,当你伤害一个时,事实上,你会伤害所有的敌人 .

    每个敌人都必须有自己的 Health . 删除 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();
    }
    

相关问题