Unity人物框选功能实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.VisualScripting;
using UnityEngine.AI;

public class BoxScaele : MonoBehaviour
{
    Vector3 startPos;//起始位置

    bool isDrag = false;//是否拖拽中

    Rect rect;

    bool isHp;

    RaycastHit hit;

    float time;

    GameObject kuang;//框

    public List<GameObject> games = new List<GameObject>();//被选中人物的集合

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPos = Input.mousePosition;
            isDrag = true;
        }

        if (isDrag)
        {
            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 + w / 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))
        {
            foreach (var item in games)
            {
                GameObject go = item.transform.Find("Yuan").gameObject;
                Destroy(go.gameObject);
            }
            games.Clear();
            foreach (var item in CreatPeon.objects) //人物集合
            {
                Vector3 pos = Camera.main.WorldToScreenPoint(item.transform.position);
                if (rect.Contains(pos))
                {
                    games.Add(item);
                    GameObject yuna = Instantiate(Resources.Load<GameObject>("Yuan"));
                    yuna.name = "Yuan";
                    yuna.transform.position = item.transform.position;
                    yuna.transform.parent = item.transform;
                }
            }
            GetComponent<RectTransform>().sizeDelta = Vector2.zero;
            isDrag = false;
        }

        if (Input.GetMouseButtonDown(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float ang = (2 * Mathf.PI) / games.Count;
            if (Physics.Raycast(ray, out hit))
            {
                for (global::System.Int32 i = 0; i < games.Count; i++)
                {
                    float x = Mathf.Sin(i * ang) * 1;
                    float z = Mathf.Cos(i * ang) * 1;
                    if (hit.transform.gameObject.tag != "Kuang")
                    {
                        games[i].GetComponent<NavMeshAgent>().SetDestination(hit.point + new Vector3(x, 0, z));
                        if (Vector3.Distance(games[i].transform.position, hit.point) <= 1)
                        {
                            games[i].GetComponent<Animator>().SetBool("Run", false);
                        }
                        else
                        {
                            games[i].GetComponent<Animator>().SetBool("Run", true);
                        }
                    }
                    else
                    {
                        games[i].GetComponent<NavMeshAgent>().SetDestination(hit.point + new Vector3(x, 0, z));
                        kuang = hit.transform.gameObject;
                        if (Vector3.Distance(games[i].transform.position, hit.point) <= 2)
                        {
                            games[i].GetComponent<Animator>().SetBool("Run", false);
                        }
                        else
                        {
                            games[i].GetComponent<Animator>().SetBool("Run", true);
                        }
                    }
                }
            }
        }
    }
}

效果

框:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值