Unity 3D - 围绕圆周向前移动对象

时间:2021-03-08 11:12:22

标签: c# unity3d

我一直在使用 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);

    }

   

1 个答案:

答案 0 :(得分:0)

transform.forward 替换为 transform.left