我们正在做一个狙击游戏,然后要模仿一个狙击镜打开时候呼吸效果,也就是狙击镜会上下左右平缓位移,我参考网上的脚本又更改了一下,你可以直接挂在你的镜头上,然后屏幕中间放一个瞄准镜,就能看到很酷的效果~
public void Noise()
{
noisedeltaX += ((((Mathf.Cos(Time.time) * Random.Range(-nosieMaxRange, nosieMaxRange) / 5f) * nosieMaxSpeed) - noisedeltaX) / 100);
noisedeltaY += ((((Mathf.Sin(Time.time) * Random.Range(-nosieMaxRange, nosieMaxRange) / 5f) * nosieMaxSpeed) - noisedeltaY) / 100);
rotationXtemp += (noisedeltaX * breathHolderVal);
rotationYtemp += (noisedeltaY * breathHolderVal);
rotationX += ((rotationXtemp - rotationX) / 3) ;
rotationY += ((rotationYtemp - rotationY) / 3) ;
if (rotationX >= 360)
{
rotationX = 0;
rotationXtemp = 0;
}
if (rotationX <= -360)
{
rotationX = 0;
rotationXtemp = 0;
}
rotationX = ClampAngle(rotationX, -180, 180);
rotationY = ClampAngle(rotationY, -60, 60);
rotationYtemp = ClampAngle(rotationYtemp, -60, 60);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.left);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.forward);
transform.localRotation = transform.localRotation * xQuaternion * yQuaternion;
}
static float ClampAngle(float angle, float min, float max)
{
if (angle <= -360.0f)
angle += 360.0f;
if (angle >= 360.0f)
angle -= 360.0f;
return Mathf.Clamp(angle, min, max);
}