Unity 3D -在一个圆的圆周上移动一个对象



我使用Unity3D将玩家始终围绕一个朝向中心的大圆的圆周移动。

下面的代码通过使用transform.forward来工作
// Rotate the forward vector towards the target direction
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);

然后我用

把玩家移到一边
characterController.Move(horizontalSpeed * transform.right * Time.deltaTime);

我一直纠结于如何让玩家在圆周上移动,但面对着前方,玩家的左边面对着圆圈的中心。我的代码如下

void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{

// Determine which direction to rotate towards
Vector3 targetDirection = GameObject.Find("Platforms").transform.position - transform.position;
// The step size is equal to speed times frame time.
float singleStep = 1 * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
// Draw a ray pointing at our target in
Debug.DrawRay(transform.position, newDirection*50, Color.red);
// Calculate a rotation a step closer to the target and applies rotation to this object
transform.rotation = Quaternion.LookRotation(newDirection);
CheckIfOnGround();
characterController.Move(velocity * Time.deltaTime);
}

transform.forward替换为transform.left

相关内容

  • 没有找到相关文章

最新更新