通过控制摄像机来模拟模型的滑动 using System.Collections;using System.Collections.Generic;using UnityEngine;public class ChinarSmoothUi3DCamera : MonoBehaviour { public Transform point; private Vector3 Tras = Vector3.zero; pub
通过控制摄像机来模拟模型的滑动
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChinarSmoothUi3DCamera : MonoBehaviour { public Transform point; private Vector3 Tras = Vector3.zero; public float distance = 10.0f; public float minDistance = 2f; public float maxDistance = 15f; public float zoomSpeed = 1f; public float xSpeed = 250.0f; public float ySpeed = 250.0f; public bool allowYTilt = true; public float yMinLimit = -90f; public float yMaxLimit = 90f; private float x = 0.0f; private float y = 0.0f; private float targetX = 0f; private float targetY = 0f; public float targetDistance = 0f; private float xVelocity = 1f; private float yVelocity = 1f; private float zoomVelocity = 1f; void Start () { Vector3 tange = transform.eulerAngles; targetX = x = tange.x; //targetY = y = ClampAngle(tange.y, yMinLimit, yMaxLimit); targetDistance = distance; } // Update is called once per frame void LateUpdate() { if(point==null) { return; } if(Input.GetAxis("Mouse ScrollWheel")>0) { targetDistance -= zoomSpeed; } if(Input.GetAxis("Mouse ScrollWheel")<0) { targetDistance += zoomSpeed; } targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance); if(Input.GetMouseButton(1)) { targetX+= Input.GetAxis("Mouse X") * xSpeed * 0.02f; if (allowYTilt) { //targetY -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f; //targetY= ClampAngle(targetY, yMinLimit, yMaxLimit); } } x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f); //y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY; //Quaternion rotation = Quaternion.Euler(y, x, 0); Quaternion rotation = Quaternion.Euler(0, x, 0); distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f); Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + point.position + Tras; transform.rotation = rotation; transform.position = position; } public float ClampAngle(float angle,float min,float max) { if(angle>360) { angle -= 360; } if(angle<-360) { angle += 360; } return Mathf.Clamp(angle, min, max); } }