Unity 2d碰撞检测c#作战

时间:2014-04-07 20:05:27

标签: c# unity3d collision-detection

我编写了一些代码来检测与敌人的物体碰撞,但是我在将几段代码拼凑成一块时遇到了一些麻烦。代码的想法是在玩家和敌人之间开始战斗,并且每1.5秒随机减少5到10点伤害。

using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {
        float playerDamage;
    float attackTimer;
    bool playerCollide;
    public GameObject Slime;
    public float enemyCurrentHealth;

    // Use this for initialization
    void Start () 
    {
        attackTimer = 1.5f;
        enemyCurrentHealth = 100.0f;
    }

    // Update is called once per frame
    void Update () 
    {
        OnCollsionEnter2D();
        if ( playerCollide )
        {
            if ( attackTimer == 1.5f )
            {
                playerDamage = Random.Range(5.0f,10.0f);
                enemyCurrentHealth -= playerDamage;
            }
            else if ( attackTimer <= 0.0f ) 
            {
                attackTimer = 1.5f;
            }
            attackTimer -= Time.deltaTime;
        }

        if ( enemyCurrentHealth <= 0 )
            Destroy (gameObject.tag == "Slime");
            enemyCurrentHealth = 100.0f;
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
            playerCollide = true;
    }

}

0 个答案:

没有答案