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);
}
}
}
}
}
}
}
效果
框: