sing UnityEngine;
public class FllowPlayer : MonoBehaviour
{
private Transform player;
private Vector3 offsetPosition;
public float scrollSpeed = 10;
public float rotateSpeed = 5;
private float distance;
private bool isRotating = false;
void Awake()
{
player = GameObject.FindGameObjectWithTag(Tags.Player).transform;
}
void Start()
{
offsetPosition = transform.position - player.position;
transform.LookAt(player.position);
;
}
void Update()
{
transform.position = player.position + offsetPosition;
//鼠标放大和缩放界面
ScrollView();
//旋转界面
RotateView();
}
//放大和缩放界面
private void ScrollView()
{
distance = offsetPosition.magnitude;
distance -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
distance = Mathf.Clamp(distance, 3, 15);
offsetPosition = offsetPosition.normalized * distance;
}
//按鼠标右键旋转界面
private void RotateView()
{
if (Input.GetMouseButtonDown(1))
{
isRotating = true;
}
if (Input.GetMouseButtonUp(1))
{
isRotating = false;
}
if (isRotating)
{
//左右旋转
transform.RotateAround(player.position, player.up, Input.GetAxis("Mouse X") * rotateSpeed);
Vector3 origionPosition = transform.position;
Quaternion origionRotation = transform.rotation;
//上下旋转
transform.RotateAround(player.position, transform.right, -Input.GetAxis("Mouse Y") * rotateSpeed);
float x = transform.eulerAngles.x;
if (x < 30 || x > 80)
{
transform.position = origionPosition;
transform.rotation = origionRotation;
}
offsetPosition = transform.position - player.position;
}
}
}
---------------------
作者:cchoop
来源:CSDN
原文:https://blog.csdn.net/qq_34937637/article/details/81238375
版权声明:本文为博主原创文章,转载请附上博文链接!