首页 > 解决方案 > 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);

    }

   

标签: c#unity3d

解决方案


替换transform.forwardtransform.left


推荐阅读