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在摄像机下的抖动