照相机线性保险杠Stuttery

我从互联网上复制了一个脚本并改变了一下:

void LateUpdate() { if (target) { x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); RaycastHit hit; //Over here is probably where the problem occurs if (Physics.Linecast(target.position, transform.position, out hit)) { if (!hit.collider.CompareTag("Player")) { distance = hit.distance; } } else { distance = 10f; } Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = rotation * negDistance + target.position; transform.rotation = rotation; transform.position = position; } } 

我做到了这一点,如果RaycastHitcollidernull ,那么距离将被重置为10.每当RaycastHit被击中时,它将移动距离玩家更近的距离,以便在阻挡视图的对象前面。 所有漂亮和花花公子,但是当我运行游戏时,当我在前面和对象,它按计划运作,但它是非常stuttery,我有一个预感,它与线性广播if语句有关,但我有不知道如何解决它,我试着将LateUpdate更改为FixedUpdateUpdate但仍然发生。 如果这是一个简单的错误,我还是对新的统一和C#来说,原谅我。

下面是一个video显示它正在发生: https : //www.youtube.com/watch?v = n6ffVmq1UWs

我不太确定以下几点。

而不是在摄像机的当前位置和目标之间进行线性广播,而是在所需的位置和目标之间进行线播。

 void LateUpdate() { if (target) { // Reset to the desired distance distance = 10 ; // OR the following line if you want smoother reset // distance = Mathf.Lerp(distance, 10, Time.deltaTime * 5); // Rotate according to mouse position x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); // Compute the desired position Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); Vector3 position = rotation * negDistance + target.position; RaycastHit hit; // Check if there is an obstacle between the desired position // and the target if (Physics.Linecast(target.position, position, out hit) && !hit.collider.CompareTag("Player")) { // Obstacle detected ! // Computing the new position so that // the object is on the other side of the obstacle distance = hit.distance; negDistance = new Vector3(0.0f, 0.0f, -hit.distance); position = rotation * negDistance + target.position; } // Placing the object transform.rotation = rotation; transform.position = position; } }