检查敌人位置是否可攻击

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("位置: 右下");
        
    }

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容