- 最后登录
- 2017-5-15
- 注册时间
- 2012-3-1
- 阅读权限
- 90
- 积分
- 32973
- 纳金币
- 32806
- 精华
- 12
|
Fps里的相机自带的一个script 叫Mouse Look,我把它改了改,使其相机视角依旧随着鼠标滑动,但前提是按下鼠标左键:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook0 : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
public float mousex;
public float mousey;
public float lastmousex=0F;
public float lastmousey=0F;
Quaternion originalRotation;
void Update ()
{
if (Input.GetMouseButton(0))
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
mousex=Input.GetAxis("Mouse X");
mousey=Input.GetAxis("Mouse Y");
rotationX += (mousex-lastmousex)* sensitivityX;
rotationY += (mousey-lastmousey) * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
mousex=Input.GetAxis("Mouse X");
rotationX += (mousex-lastmousex)* sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
mousey=Input.GetAxis("Mouse Y");
rotationY += (mousey-lastmousey) * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
想请教的是,如果想要给相机视角加上缓动的效果~
即,相机的视角随着鼠标不是立即停止或启动 ,而是 每当鼠标开始移动或停止,视角都有个相应的加速或减速的过程,
该如何实现? 提供个思路亦可。XD~
|
|