116 lines
4.0 KiB
C#
116 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class Move : MonoBehaviour,IPointerDownHandler, IPointerUpHandler
|
|
{
|
|
public Camera camera;
|
|
|
|
public int direction; // 方向 1:上 2:下 3:左 4:右
|
|
private bool isMoving=false;
|
|
public BoxCollider boxCollider;
|
|
private Vector3 minBounds;
|
|
private Vector3 maxBounds;
|
|
private float rotationY = 0.0f; // Y轴旋转角度
|
|
|
|
// 接近目标
|
|
public List<GameObject> targets; // 目标物体
|
|
|
|
void Start()
|
|
{
|
|
// 计算边界
|
|
minBounds = boxCollider.bounds.min;
|
|
maxBounds = boxCollider.bounds.max;
|
|
}
|
|
void Update()
|
|
{
|
|
// 如果正在移动,向前移动摄像机
|
|
if (direction==1 && isMoving && camera != null)
|
|
{
|
|
if (targets!= null && targets.Count > 0)
|
|
{
|
|
foreach (GameObject target in targets)
|
|
{
|
|
float distance = Vector3.Distance(camera.transform.position, target.transform.position);
|
|
if (distance < 0.5f)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
Vector3 newPosition= camera.transform.forward * 2f * Time.deltaTime+camera.transform.position;
|
|
// 限制物体位置在边界内
|
|
newPosition.x = Mathf.Clamp(newPosition.x, minBounds.x, maxBounds.x);
|
|
newPosition.z = Mathf.Clamp(newPosition.z, minBounds.z, maxBounds.z);
|
|
|
|
camera.transform.position = newPosition;
|
|
}
|
|
if (direction==2 && isMoving && camera != null)
|
|
{
|
|
|
|
Vector3 newPosition= camera.transform.forward * -2f * Time.deltaTime+camera.transform.position;
|
|
// 限制物体位置在边界内
|
|
newPosition.x = Mathf.Clamp(newPosition.x, minBounds.x, maxBounds.x);
|
|
newPosition.z = Mathf.Clamp(newPosition.z, minBounds.z, maxBounds.z);
|
|
|
|
camera.transform.position = newPosition;
|
|
}
|
|
if (direction==3 && isMoving && camera != null)
|
|
{
|
|
|
|
Vector3 newPosition= camera.transform.right * -2f * Time.deltaTime+camera.transform.position;
|
|
// 限制物体位置在边界内
|
|
newPosition.x = Mathf.Clamp(newPosition.x, minBounds.x, maxBounds.x);
|
|
newPosition.z = Mathf.Clamp(newPosition.z, minBounds.z, maxBounds.z);
|
|
|
|
camera.transform.position = newPosition;
|
|
}
|
|
if (direction==4 && isMoving && camera != null)
|
|
{
|
|
|
|
Vector3 newPosition= camera.transform.right * 2f * Time.deltaTime+camera.transform.position;
|
|
// 限制物体位置在边界内
|
|
newPosition.x = Mathf.Clamp(newPosition.x, minBounds.x, maxBounds.x);
|
|
newPosition.z = Mathf.Clamp(newPosition.z, minBounds.z, maxBounds.z);
|
|
camera.transform.position = newPosition;
|
|
}
|
|
if (direction==5 && isMoving && camera != null)
|
|
{
|
|
camera.transform.Rotate(Vector3.up, 50f * Time.deltaTime);
|
|
}
|
|
if (direction==6 && isMoving && camera != null)
|
|
{
|
|
camera.transform.Rotate(Vector3.up, -50f * Time.deltaTime);
|
|
}
|
|
|
|
// 检测右键是否被按下
|
|
if (Input.GetMouseButton(0)) // 1表示右键
|
|
{
|
|
// 获取鼠标移动的增量
|
|
float mouseX = Input.GetAxis("Mouse X");
|
|
rotationY = camera.transform.localEulerAngles.y;
|
|
// 计算旋转
|
|
rotationY += mouseX * 1f;
|
|
|
|
// 应用旋转到摄像头
|
|
camera.transform.localRotation = Quaternion.Euler(0, rotationY, 0);
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
isMoving = true;
|
|
gameObject.GetComponent<Image>().color = new Color(1, 1, 1, 1f);
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
isMoving = false;
|
|
gameObject.GetComponent<Image>().color = new Color(1, 1, 1, 0.2f);
|
|
}
|
|
}
|