从统一的标准编程资产旋转第三人称角色

问题描述 投票:0回答:1

我想自动化一个团结第三人称角色的随机轮换,我想旋转是动画,如果我用了开箱控制器和WASD键的转动玩家自己。我们的目标是,随机旋转,找人在人群中的NPC。

以下是我迄今为止尝试,在更新协程之内。

float xDegress = Random.Range(10f, 75f);
float yDegress = Random.Range(10f, 75f);
float zDegress = Random.Range(10f, 75f);

// Works but immediate
this.transform.Rotate(0f, yDegress, 0f);

// Works but immediate
this.transform.LookAt(new Vector3(xDegress, 0f, zDegress));

// Doesn't work 
rB.rotation = Quaternion.Lerp(transform.rotation, new Quaternion(0, yDegress, 0, 0), 0.5f);

// Doesn't work 
rB.MoveRotation(new Quaternion(0, yDegress, 0, 1).normalized);

// Works but immediate
Vector3 newPosition = new Vector3(xDegress, 0f, zDegress);
transform.rotation = Quaternion.Lerp(
    transform.rotation,
    Quaternion.LookRotation(newPosition, Vector3.up),
    0.5f);

yield return new WaitForSeconds(_pauseSeconds);

下面是全国人民代表大会,这是阮经天从标准的资产检查。

enter image description here

unity3d 3d
1个回答
0
投票

首先,因为它是一个RigidBody你不应该使用transform.rotation =也没有任何transform.方法可言,但坚持rb.MoveRotationrb.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);
© www.soinside.com 2019 - 2024. All rights reserved.