【问题标题】:Rotating a 3D object in a 2d scene在 2d 场景中旋转 3D 对象
【发布时间】:2015-09-14 15:16:43
【问题描述】:

我正在制作一个 2.5D 平台游戏,但我无法正确旋转玩家。我只希望它在玩家左右移动时在 X 轴上旋转。我的动作脚本是这样的:

      if (isWalking)
    {
        transform.Rotate(0, facingDir, 0);
        isWalking = false;
    }
    else { }

    if (Input.GetKey(KeyCode.Space))
        GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jumpHeight);

    if (Input.GetKey(KeyCode.A)){
        GetComponent<Rigidbody>().velocity = new Vector2(-speedHeight, GetComponent<Rigidbody>().velocity.y);
        facingDir = 180;
        isWalking = true;
    }

    if (Input.GetKey(KeyCode.D))
    {
        GetComponent<Rigidbody>().velocity = new Vector2(speedHeight, GetComponent<Rigidbody>().velocity.y);
        facingDir = 0 ;
        isWalking = true;
    }

我可以旋转它的最好方法是通过 transform.rotate(0,180,0) 和 (0,0,0),但是它会不停地旋转,我怎么知道玩家在 X 上移动的方向-axis 这样我就可以正确地转换。旋转?

【问题讨论】:

    标签: unity3d


    【解决方案1】:
    transform.forward
    

    这应该为您提供玩家前进的方向。您可以使用

    transform.LookAt(target);
    

    将玩家定向到特定方向。主要问题是您试图更新 GetKey 函数中的方向,该函数在按住键时返回 true。这会导致角色不断旋转,您可以通过使用额外的检查来避免这种情况。您可以通过以下方式使用 Lookat:

    GetComponent<Rigidbody>().velocity = new Vector2(-speedHeight, GetComponent<Rigidbody>().velocity.y);
    
    transform.LookAt(transform.position+new Vector3(GetComponent<Rigidbody>().velocity.x,0,GetComponent<Rigidbody>().velocity.y));
    

    有更好的方法可以做到这一点,但这应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多