unity 震动脚本_Unity 摄像机跟随脚本,摄像机抖动问题?

public void FindPlayer()

{

TargetLookAt = GameObject.FindGameObjectWithTag("Player").transform;

if(TargetLookAt==null)

{

Debug.Log("no player");

return;

}

}

public void Reset()

{

MouseX = 0;

MouseY = 10;

CurrentDistance = StartDistance;

DesiredDistance = CurrentDistance;

}

void HandlePlayerInput()

{

var deadZone = 0.05f;

if(Input.GetMouseButton(1))

{

MouseX += Input.GetAxis("Mouse X")*X_MouseSensitivity;

MouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;

}

MouseY = CheckYLimit(MouseY, Y_MinAngle, Y_MaxAngle);

var currentScrollWheel = Input.GetAxis("Mouse ScrollWheel");

if (currentScrollWheel deadZone)

{

DesiredDistance = Mathf.Clamp(CurrentDistance - currentScrollWheel * MouseWheelSensitivity

, DistanceMin, DistanceMax);

}

}

void CalculateDesiredPosition()

{

CurrentDistance = Mathf.SmoothDamp(CurrentDistance, DesiredDistance, ref MovingVelocity, DistanceSmooth);

DesiredPosition = CalculatePosition(MouseY, MouseX, CurrentDistance);

}

Vector3 CalculatePosition(float rotationX,float rotationY,float distance)

{

Vector3 direction = new Vector3(0, 0, -distance);

Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);

return TargetLookAt.position + rotation * direction;

}

void UpdatePosition()

{

CurrentPosition = transform.position;

Xpos = Mathf.SmoothDamp(CurrentPosition.x, DesiredPosition.x, ref X_Velocity, X_Smooth);

Ypos = Mathf.SmoothDamp(CurrentPosition.y, DesiredPosition.y, ref Y_Velocity, Y_Smooth);

Zpos = Mathf.SmoothDamp(CurrentPosition.z, DesiredPosition.z, ref Z_Velocity, Z_Smooth);

Vector3 targetPos = new Vector3(Xpos, Ypos, Zpos);

transform.position = targetPos;

transform.LookAt(TargetLookAt);

}

float CheckYLimit(float angel,float min,float max)

{

do

{

if (angel < -360)

angel += 360;

if (angel > 360)

angel -= 360;

} while (angel360);

return Mathf.Clamp(angel, min, max);

}

}

以上,把这个脚本挂到摄像机上,拖一个Unity内置的角色控制器到场景里,把角色Tag为Player,就可以实现角色跟随和脚本其他功能,但是却会有一些抖动,尤其在Scene视图下面,可以看到摄像机的Clip视图框有抖动的现象,这到底是哪里出问题了?

我试过了把

public float X_Smooth = 0.05f;

public float Y_Smooth = 0.05f;

public float Z_Smooth = 0.05f;

改为一个更小的值或者0有一点效果,可是人物转身和改变方向时还是有点不流畅是怎么回事?难道是我太强迫症了...............

可以在场景里添加几个Cube,当角色在Cube旁转身或徘徊时可以看到Cube在摄像机下的抖动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值