У меня есть попробовал использовать эту функцию прямо здесь, чтобы получить целевое значение, которое будет использоваться для поворота камеры вокруг автомобиля, но функция не возвращает правильные значения, если скорость превышает минимальный порог, который я установил в инспекторе. Если это значение неверно, я не смогу правильно повернуть камеру.
Код: Выделить всё
public Vector3 GetFollowOffsetPosition(Vector3 followerPosition, Vector3 followObjectPosition, Vector3 followObjectVelocity, float followDistance)
{
// If the followed object is stationary, just return the current position (no offset).
if (followObjectVelocity.magnitude < minSpeedThreshold)
{
return defaultCamPosition; // No significant velocity, return current follower position.
}
// Get the direction of the followed object's velocity (ignoring y component for horizontal following).
Vector3 velocityDirection = followObjectVelocity.normalized;
// Calculate the offset position: followObject's position minus the offset along the velocity direction.
Vector3 offsetPosition = (new Vector3(Mathf.Sign(velocityDirection.z) * velocityDirection.x , defaultCamPosition.y, Mathf.Sign(velocityDirection.z) * followDistance) );
// Return the calculated offset position.
offsetPosition.y = defaultCamPosition.y;
return offsetPosition;
}
Код: Выделить всё
private void FixedUpdate()
{
if (hasRigidBody)
{
followPointVelocity = rb.velocity; //get the Rigidbody velocity
}
else
{
followPointVelocity = (followObject.transform.position - lastPosition) / Time.fixedDeltaTime; //get velocity without a rigidbody.
}
Vector3 relativeVelocity = followObject.transform.InverseTransformDirection(followPointVelocity); //get velocity value in local coordinates of the follow object
followPointAcceleration = (followPointVelocity - lastVelocity) / Time.fixedDeltaTime; //get Acceleration values.
Vector3 relativeAcceleration = followObject.transform.InverseTransformDirection(followPointAcceleration); // get the acceleration in local coordinates of the follow object
Vector3 targetPosition = followObject.transform.TransformPoint(camPositionOffset);
Vector3 camLocalPosition = followObject.transform.InverseTransformPoint(cam.transform.position);
Vector3 targetLocalPosition = followObject.transform.InverseTransformPoint(targetPosition);
float newLocalX = Mathf.SmoothDamp(camLocalPosition.x, targetLocalPosition.x, ref velocityX, smoothTimeX);
float newLocalY = Mathf.SmoothDamp(camLocalPosition.y, targetLocalPosition.y, ref velocityY, smoothTimeY);
float newLocalZ = Mathf.SmoothDamp(camLocalPosition.z, targetLocalPosition.z, ref velocityZ, smoothTimeZ);
Vector3 newLocalPosition = new Vector3(newLocalX, newLocalY, newLocalZ);
Vector3 smoothedWorldPosition = followObject.transform.TransformPoint(newLocalPosition);
cam.transform.position = smoothedWorldPosition;
camPositionOffset = Vector3.SmoothDamp(camPositionOffset, GetFollowOffsetPosition(camPositionOffset, Vector3.zero, relativeVelocity, camPositionOffset.z),ref velocity, smoothTime);
cam.transform.LookAt(followObject.transform);
lastPosition = followObject.transform.position;
lastVelocity = followPointVelocity;
lastAcceleration = followPointAcceleration;
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ven-distan