Прежде всего, поскольку это RigidBody
, вы не должны использовать transform.rotation =
или какой-либо transform.
метод вообще, но придерживайтесь rb.MoveRotation
или rb.rotation
.
, чем вы использовали Lerp
"неправильным" образом.То, что вы хотите, это значение, которое увеличивается от 0
до 1
.Вызов его только один раз с фиксированным коэффициентом 0.5f
всегда приводит к повороту на полпути между первым и вторым - немедленно.
Сначала вам нужна желаемая продолжительность вращения.
// How long in seconds should the rotation take?
// might be a constant or set via the inspector
public float rotationDuration = 1.0f;
Чем раньше вы начали вращение, получите начальное и конечное вращение
float xDegress = Random.Range(10f, 75f);
float yDegress = Random.Range(10f, 75f);
float zDegress = Random.Range(10f, 75f);
var currentRotation = transform.rotation;
var targetRotation = currentRotation * Quaternion.Euler(xDegress, yDegress, zDegress);
// or
Vector3 newPosition = new Vector3(xDegress, 0f, zDegress);
var targetRotation = Quaternion.LookRotation(newPosition, Vector3.up);
// or whatever your target rotation shall be
Теперь, так как вы уже находитесь в сопрограмме, вы можете просто добавить цикл while
, выполняющий вращение в течение требуемой продолжительности(не забывайте, хотя yield
)
var passedTime = 1.0f;
while(passedTime < rotationDuration)
{
// interpolate the rotation between the original and the targetRotation
// factor is a value between 0 and 1
rb.MoveRotation = Quaternion.Lerp(currentRotation, targetRotation, passedTime / rotationDuration);
// add time passed since last frame
passedtime += Time.deltaTime;
yield return null;
}
// to avoid overshooting set the target rotation fix when done
transform.rotation = targetRotation;
yield return new WaitForSeconds(_pauseSeconds);