框选3D物体+群体导航

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

public class CreateKuang : MonoBehaviour
{
    Rect rect;
    Vector3 startpos;
    Vector3 endpos;
    public GameObject playlist;
    Camera ca;
    List<GameObject> playlists = new List<GameObject>();
    float anger;
    // Start is called before the first frame update
    void Start()
    {
        ca = Camera.main;
        rect = this.GetComponent<RectTransform>().rect;
    }
    bool isdraw;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startpos = Input.mousePosition;
            isdraw = true;
        }
        if (isdraw)
        {
            float w = Mathf.Abs(startpos.x - Input.mousePosition.x);
            float h = Mathf.Abs(startpos.y - Input.mousePosition.y);
            GetComponent<RectTransform>().sizeDelta = new Vector2(w, h);
            float x = startpos.x < Input.mousePosition.x ? startpos.x + w / 2 : Input.mousePosition.x + w / 2;
            float y = startpos.y < Input.mousePosition.y ? startpos.y + h / 2 : Input.mousePosition.y + h / 2;
            GetComponent<RectTransform>().anchoredPosition = new Vector2(x - Screen.width / 2, y - Screen.height / 2);
            rect = new Rect(x-w/2, y-h/2, w, h);
        }

        if (Input.GetMouseButtonUp(0))
        {
            GetComponent<RectTransform>().sizeDelta = Vector2.zero;
            isdraw = false;

            for (int i = 0; i < playlist.transform.childCount; i++)
            {
                playlist.transform.GetChild(i).GetComponent<Player>().quan.SetActive(false);
            }
            playlists.Clear();                 
            for (int i = 0; i < playlist.transform.childCount; i++)
            {
                Vector3 pos = ca.WorldToScreenPoint(playlist.transform.GetChild(i).position);
                if (rect.Contains(pos))
                {
                    playlists.Add(playlist.transform.GetChild(i).gameObject);
                    playlist.transform.GetChild(i).GetComponent<Player>().quan.SetActive(true);
                }
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out RaycastHit hit))
            {
                anger = (2 * Mathf.PI) / playlists.Count;

                for (int i = 0; i < playlists.Count; i++)
                {
                    float x = Mathf.Sin(anger * i) * 1;
                    float z = Mathf.Cos(anger * i) * 1;
                    if (playlists.Count <=2 )
                    {
                        playlists[i].GetComponent<Player>().an.SetDestination(hit.point);
                    }
                    else
                    {
                        playlists[i].GetComponent<Player>().an.SetDestination(hit.point + new Vector3(x, 0, z));
                    }
                  
                }

            }
           
        }

    }
    public void FaShe(Vector3 target)
    {
        for(int i = 0; i < playlists.Count; i++)
        {
            GameObject daodan = Instantiate(Resources.Load<GameObject>("Sphere"));
            daodan.transform.position = playlists[i].transform.position;
            daodan.GetComponent<daodan>().pos3 = target;
        }
    }

  

}

很多游戏都会用到框选以及群体导航移动,比如魔兽争霸,红警,导航控制小兵。

做框选的时候要创建一个Image,然后让他中间镂空,方法在下边

 框选其实就是当你鼠标摁下的坐标为Image初始位置,然后计算你鼠标划出去的距离,转换为Image的长和宽高,当鼠标抬起的时候获取中心镂空内物体的坐标 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值