最近做了一个鼠标拖拽360°旋转查看物体的功能,很基础的一个功能,代码不多写的很快 但是最后出来的效果不是很满意,感觉没有灵魂,很死板僵硬,所以做了一点小修改,在鼠标拖拽结束时计算最后鼠标的加速度,让物体在继续旋转一段距离
Vector3 Cube_MomentRato_Speed;
public float Cube_Rato_Speed=0.2f;
public float Decrease = 0.2f;
void Update()
{
Cube_Rotation();
}
void Cube_Rotation()
{
float Mouse_x = Input.GetAxis("Mouse X");
float Mouse_y = Input.GetAxis("Mouse Y");
Vector3 mouve = new Vector3(Mouse_y,Mouse_x,0);
if (Input.GetMouseButton(0))
{
Cube_MomentRato_Speed = mouve / Time.deltaTime;
}
else
{
Cube_MomentRato_Speed *= Mathf.Pow(Decrease,Time.deltaTime);
if (Mathf.Abs(Vector3.Magnitude(Cube_MomentRato_Speed)) < 1)
{
Cube_MomentRato_Speed = Vector3.zero;
}
}
transform.Rotate(Vector3.up, -Cube_MomentRato_Speed.y * Cube_Rato_Speed, Space.World);
transform.Rotate(Vector3.right, Cube_MomentRato_Speed.x * Cube_Rato_Speed, Space.World);
}