检查敌人位置是否可攻击

2016-06-09  本文已影响22人  自由的天空

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour {

public float speed = 2;

public GameObject enemy;
public Dictionary<string,GameObject> enemyList = new Dictionary<string,GameObject>();

public TextMesh text1;
public TextMesh text2;

//判断敌人是否可以攻击
public void CanAttack(GameObject obj)
{
    Vector3 toOther = this.transform.position - obj.gameObject.transform.position;
    Vector3 chaCheng1 = Vector3.Cross(transform.forward, toOther);
    Vector3 chaCheng2 = Vector3.Cross(transform.right, toOther);

    //调试距离的代码
    text1.text = chaCheng1.ToString ();
    text2.text = chaCheng2.ToString ();

    //经过调试 前后距离是3 左右距离是2,
    //当敌人在左上或右上 并且 距离小于攻击距离, 就可以攻击


    if (chaCheng1.y > 0 && chaCheng2.y > 0) 
    {
        Debug.Log("位置: 左上");
        if(Mathf.Abs(chaCheng2.y) <= 3 && Mathf.Abs(chaCheng1.y) <= 2)
        {
            //打开头顶可攻击标示
            obj.gameObject.transform.FindChild ("T").gameObject.SetActive(true);
        }else
        {
            obj.gameObject.transform.FindChild ("T").gameObject.SetActive(false);
        }

        return;
    }
    if (chaCheng1.y < 0 && chaCheng2.y > 0)
    {
        Debug.Log("位置: 右上");
        
        if(Mathf.Abs(chaCheng2.y) <= 3 && Mathf.Abs(chaCheng1.y) <= 2)
        {
            obj.gameObject.transform.FindChild ("T").gameObject.SetActive(true);
        }else
        {
            obj.gameObject.transform.FindChild ("T").gameObject.SetActive(false);
        }
        return;
    }

    obj.gameObject.transform.FindChild ("T").gameObject.SetActive(false);


    if (chaCheng1.y > 0 && chaCheng2.y < 0)
    {
        Debug.Log("位置: 左下");
        return;
    }


    if (chaCheng1.y < 0 && chaCheng2.y < 0)
    {
        Debug.Log("位置: 右下");
        
    }

}

}

上一篇下一篇

猜你喜欢

热点阅读