- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
using UnityEngine;
//用于产品展示类的相机脚本,拖拽旋转.滚轮放大缩小.带惯性缓冲效果.
public class MouseFollowRotation : MonoBehaviour
{
public Transform target;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;
public float yMinLimit = -50, yMaxLimit = 50;
public float distance = 2, minDistance = 2, maxDistance = 30;
public bool needDamping = true;
float damping = 5.0f;
public float x = 0.0f;
public float y = 0.0f;
public void SetTarget(GameObject go)
{
target = go.transform;
}
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void Update()
{
if (target)
{
//鼠标左键旋转相机
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
//print(Input.GetAxis("Mouse X"));
//print( Input.GetAxis("Mouse Y"));
//print(x);
//print(y);
}
//鼠标滚轮拉近拉远相机
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
//计算相机新的旋转角度和距离位置
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * disVector + target.position;
//调整相机
if (needDamping) //是否使用缓冲效果
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
//鼠标右键移动目标物体
if (Input.GetMouseButton(1))
{
target.transform.Translate(Time.deltaTime * this.mSpeed *Input.GetAxis("Mouse X"), 0, 0, Camera.main.transform);
target.transform.Translate(0, Time.deltaTime * this.mSpeed * Input.GetAxis("Mouse Y"), 0);
}
return;
}
}
//钳制角度
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
|
|