using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
//需要“挤开”的物体数组
public GameObject[] targets;
//需要“挤开”的物体的边界框
Bounds[] targetBound;
//自身的边界框
Bounds bound;
//虚拟一个平面用来判断目标位于自身的左边或右边
Plane plane;
//自身与目标物体的半径和
Vector2[] dis;
//鼠标点击的点
Vector2 clickPoint = Vector2.zero;
//是否移动
bool isMove;
void Start()
{
//初始化
isMove = false;
targetBound = new Bounds[targets.Length];
dis = new Vector2[targets.Length];
bound= this.GetComponent<Renderer>().bounds;
for (int i = 0; i < targets.Length; i++)
{
targetBound[i] = targets[i].GetComponent<Renderer>().bounds;
dis[i] = bound.extents + targetBound[i].extents;
}
//print("边界盒的中心" + bound.center);
//print("边界框的实际范围。这个总是size的一半。" + bound.extents);
//print("边界盒的最大点,这个总是等于center + extents。" + bound.max);
//print("边界盒的最小点,这个总是等于center-extents。" + bound.min);
//print("边界盒的总大小。这个总是extents的两倍大。" + bound.size);
}
void Update()
{
for (int i = 0; i < targets.Length; i++)
{
if (Vector2.Distance(this.transform.position, targets[i].transform.position) < dis[i].x)
{
plane = new Plane(Vector3.left, this.transform.position);
if (plane.GetSide(targets[i].transform.position))
{
targets[i].transform.Translate(Vector2.left * Time.deltaTime * 2);
}
else
{
targets[i].transform.Translate(Vector2.right * Time.deltaTime * 2);
}
}
}
if (Input.GetMouseButtonDown(0))
{
isMove = true;
clickPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
CricleMove();
}
/// <summary>
/// 主角移动
/// </summary>
void CricleMove()
{
if (isMove)
{
this.transform.position = Vector2.MoveTowards(this.transform.position, clickPoint, Time.deltaTime * 2);
if (Vector2.Distance(this.transform.position, clickPoint) < 0.1f)
{
isMove = false;
}
}
}
}
新手不会发动图所以没法演示效果
刚入行的新手,望大佬多多斧正,谢谢!