179 lines
5.4 KiB
C#
179 lines
5.4 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class FollowPlayer : MonoBehaviour
|
||
{
|
||
bool isRotating;
|
||
//位置偏移(摄像机与人)
|
||
private Vector3 offsetPosition;
|
||
private bool dragPanMoveActive;
|
||
private float dragPanSpeed = 1f;
|
||
private Vector2 lastMousePosition;
|
||
private float distance = 0;
|
||
[Tooltip("最远视野距离 ")]
|
||
public float farDistance = 50;
|
||
[Tooltip("拉近视野速度")]
|
||
public float scrollspeed = 2;//拉近视野速度
|
||
public float rotateSpeed = 1.0f;//摄像机旋转的速度。
|
||
//[Tooltip("摄像机的最小高度 ")]
|
||
//public float camMinHeight = 0.1f;
|
||
//[Tooltip("摄像机的最大高度 ")]
|
||
//public float camMaxHeight = 20f;
|
||
public float moveSpeed = 30f;
|
||
public Transform player;
|
||
[Tooltip("控制垂直旋转的物体")]
|
||
public Transform verticalRotateObj;
|
||
public bool OnUI = false;
|
||
[SerializeField] private bool useDragPan = true;
|
||
[SerializeField] private bool LowerY = true;
|
||
|
||
void Update()
|
||
{
|
||
//处理视野的旋转
|
||
RotateView();
|
||
//处理视野的拉近和拉远的效果
|
||
if(LowerY)
|
||
ScrollView();
|
||
//处理视野平移
|
||
if(useDragPan)
|
||
MovementView();
|
||
|
||
if (EventSystem.current.IsPointerOverGameObject())
|
||
{
|
||
//print("在UI上");
|
||
OnUI = true;
|
||
|
||
}
|
||
else
|
||
{
|
||
//print("不在UI上");
|
||
OnUI = false;
|
||
}
|
||
if (OnUI == true)
|
||
{
|
||
useDragPan = false;
|
||
LowerY = false;
|
||
}
|
||
else
|
||
{
|
||
useDragPan = true;
|
||
LowerY = true;
|
||
}
|
||
}
|
||
|
||
void RotateView()
|
||
{
|
||
float rotateDir = 0f;
|
||
float rotateDirY = 0f;
|
||
|
||
if (Input.GetMouseButtonDown(1))
|
||
{
|
||
isRotating = true;
|
||
lastMousePosition = Input.mousePosition;
|
||
}
|
||
if (Input.GetMouseButtonUp(1))//1代表鼠标右键
|
||
{
|
||
isRotating = false;
|
||
|
||
}
|
||
|
||
if (isRotating)
|
||
{
|
||
float mouseMovementDelta = Input.mousePosition.x - lastMousePosition.x;
|
||
rotateDir = mouseMovementDelta * rotateSpeed;
|
||
|
||
float mouseMovementDeltaY = Input.mousePosition.y - lastMousePosition.y;
|
||
rotateDirY = mouseMovementDeltaY * rotateSpeed;
|
||
|
||
//向右滑动时正值,向左滑动是负值。
|
||
//transform.RotateAround(player.position, player.up, rotateSpeed * Input.GetAxis("Mouse X"));
|
||
|
||
//Vector3 originalPos = transform.position;
|
||
//Quaternion originalRotation = transform.rotation;
|
||
|
||
//transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));//影响的属性有两个,position,rotation;
|
||
|
||
//限制摄像机垂直滑动的距离;
|
||
//float x = transform.eulerAngles.x;
|
||
//if (x < 0 || x > 80)//当超出范围之后,我们将属性归位原来的,就是让旋转无效;
|
||
//{
|
||
// transform.position = originalPos;
|
||
// transform.rotation = originalRotation;
|
||
//}
|
||
|
||
player.eulerAngles += new Vector3(0, rotateDir * Time.deltaTime, 0);
|
||
|
||
Vector3 tempV3 = verticalRotateObj.eulerAngles - new Vector3(rotateDirY * Time.deltaTime, 0, 0);
|
||
|
||
if (tempV3.x > 0 && tempV3.x < 90)//超出范围之外的旋转无效;
|
||
{
|
||
verticalRotateObj.eulerAngles = tempV3;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
//每次更新一下。
|
||
offsetPosition = transform.position - player.position;
|
||
}
|
||
|
||
private void ScrollView()
|
||
{
|
||
//print(Input.GetAxis("Mouse ScrollWheel"));//向后 返回负值(拉近视野),向前滑动 返回正值(拉远视野)
|
||
distance = offsetPosition.magnitude;//获得距离。
|
||
distance -= Input.GetAxis("Mouse ScrollWheel") * scrollspeed;//拉近视野
|
||
if (distance > 5 && distance < farDistance)
|
||
{
|
||
offsetPosition = offsetPosition.normalized * distance;//方向不变,将长度变为distance
|
||
transform.position = offsetPosition + player.position;
|
||
}
|
||
}
|
||
|
||
void MovementView()
|
||
{
|
||
Vector3 inputDir = new Vector3(0, 0, 0);
|
||
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
dragPanMoveActive = true;
|
||
lastMousePosition = Input.mousePosition;
|
||
}
|
||
if (Input.GetMouseButtonUp(0))
|
||
{
|
||
dragPanMoveActive = false;
|
||
}
|
||
|
||
if (dragPanMoveActive)
|
||
{
|
||
Vector2 mouseMovementDelta = (Vector2)Input.mousePosition - lastMousePosition;
|
||
|
||
// float dragPanSpeed = 1f;
|
||
inputDir.x = mouseMovementDelta.x * dragPanSpeed;
|
||
inputDir.z = mouseMovementDelta.y * dragPanSpeed;
|
||
|
||
lastMousePosition = Input.mousePosition;
|
||
|
||
Vector3 moveDir = player.forward * -inputDir.z + player.right * -inputDir.x;
|
||
|
||
player.position += moveDir * moveSpeed * Time.deltaTime;
|
||
}
|
||
|
||
}
|
||
|
||
//void OnDragMove(DragGesture gesture)
|
||
//{//拖拽移动
|
||
// if (gesture.Phase == ContinuousGesturePhase.Started)
|
||
// if (gesture.Phase != ContinuousGesturePhase.Updated) return;
|
||
// Vector.x = -gesture.DeltaMove.x * moveRatio;
|
||
// Vector.y = 0;
|
||
// Vector.z = -gesture.DeltaMove.y * moveRatio;
|
||
// Vector3 temp2 = transform.TransformPoint(Vector);
|
||
// Quaternion quaTemp = Quaternion.AngleAxis(-transform.eulerAngles.x, transform.right);
|
||
// temp2 = quaTemp * (temp2 - transform.position) + transform.position;
|
||
// transform.position = temp2;
|
||
//}
|
||
|
||
} |