Unity C#:transform.up不会随着对象旋转而更新

加布·塔克(Gabe Tucker)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public GameObject player;
    public Rigidbody2D playerRB;
    public Transform playerTF;

    public float moveForce;
    public float rotateForce;

    private string currentMoveKey = "";
    private string currentRotateKey = "";

    void Update()
    {
        //move
        if(Input.GetKey("w") && currentMoveKey == "")
        {
            currentMoveKey = "w";

            playerRB.AddForce(transform.up * moveForce * Time.deltaTime);
        }

        if (Input.GetKeyUp("w") && currentMoveKey == "w")
        {
            playerRB.velocity = new Vector2(0, 0);//resets when keyup and not already reset

            currentMoveKey = "";
        }

        if (Input.GetKey("s") && currentMoveKey == "")
        {
            currentMoveKey = "s";

            playerRB.AddForce(transform.up * -moveForce * Time.deltaTime);
        }

        if (Input.GetKeyUp("s") && currentMoveKey == "s")
        {
            playerRB.velocity = new Vector2(0, 0);

            currentMoveKey = "";
        }
        //rotate
        if (Input.GetKeyDown("a") && currentRotateKey == "")
        {
            currentRotateKey = "a";
        }
        if (Input.GetKey("a") && currentRotateKey == "a")
        {
            playerTF.Rotate(transform.forward * rotateForce * Time.deltaTime);
        }
        if (Input.GetKeyUp("a") && currentRotateKey == "a")
        {
            currentRotateKey = "";
        }

        if (Input.GetKeyDown("d") && currentRotateKey == "")
        {
            currentRotateKey = "d";
        }
        if (Input.GetKey("d") && currentRotateKey == "d")
        {
            playerTF.Rotate(transform.forward * -rotateForce * Time.deltaTime);
        }
        if (Input.GetKeyUp("d") && currentRotateKey == "d")
        {
            currentRotateKey = "";
        }
    }
}

嘿!由于某些原因,在我将力添加到RigidBody2D的那一行中,如果我在移动球员的同时旋转它,从而改变了指向的位置,那么球员将继续朝着他最初指向的方向移动。换句话说,他看起来像他在滑动,我必须停止移动并重新开始移动以更新他指向的方向。我该怎么做才能解决此问题,以便在他旋转时,他的移动方向会同时更新?

NSJacob1

加力只是不断增加刚体的动量。自从您在举起琴键时重置速度后,就已经知道了这一点。当您转身但保持向前移动时,刚体速度不会被重置,而只是进行了相加更改。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public GameObject player;
    public Rigidbody2D playerRB;
    public Transform playerTF;

    public float moveForce;
    public float rotateForce;

    private Vector3 movement;

    private string currentRotateKey = "";

    void Update()
    {
        //move in Local space
        if (Input.GetKeyDown(KeyCode.W))
        {
            movement += Vector3.up;
        }

        if (Input.GetKeyUp(KeyCode.W))
        {
            movement -= Vector3.up;
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            movement -= Vector3.up;
        }

        if (Input.GetKeyUp(KeyCode.S))
        {
            movement += Vector3.up;
        }

        //rotate
        if (Input.GetKeyDown("a") && currentRotateKey == "")
        {
            currentRotateKey = "a";
        }
        if (Input.GetKey("a") && currentRotateKey == "a")
        {
            playerTF.Rotate(transform.forward * rotateForce * Time.deltaTime);
        }
        if (Input.GetKeyUp("a") && currentRotateKey == "a")
        {
            currentRotateKey = "";
        }

        if (Input.GetKeyDown("d") && currentRotateKey == "")
        {
            currentRotateKey = "d";
        }
        if (Input.GetKey("d") && currentRotateKey == "d")
        {
            playerTF.Rotate(transform.forward * -rotateForce * Time.deltaTime);
        }
        if (Input.GetKeyUp("d") && currentRotateKey == "d")
        {
            currentRotateKey = "";
        }

        // Implicit cast from Vector3 to Vector2, takes X and Ys
        ApplyMovement(movement);
    }

    public void ApplyMovement(Vector2 movement)
    {
        playerRB.velocity = player.transform.TransformDirection(movement.x, movement.y, 0) * moveForce * Time.deltaTime;
    }
}

我发现使用KeyCode枚举而不是字符串值会更干净一些,而且我喜欢Vector3 movement为该类跟踪局部变量而不是使用check currentMovementKey这样,您可以一次按下多个键,并在编译时捕获拼写错误。

将您的动作“应用”到自己的方法中有助于在需要时更改行为。

movement在局部空间中处理,使用Vector3.up而不是transform.up避免旋转时出错,然后将其应用于时,playerRB.velocity我们可以使用转换回世界空间player.transform.TransformDirection(movement.x, movement.y, 0)(也可以transform.TransformDirection(movement.x, movement.y, 0)是该组件与RigidBody2D组件位于同一GameObject上)。

如果您想保持加速度而不是想跳起来而不是掉下来,但是又不想像真实物体那样漂移,事情会变得有些棘手,这是解决问题的一种方法:

    public void ApplyMovement(Vector3 movement)
    {
        if (movement.sqrMagnitude > Mathf.Epsilon)
        {
            playerRB.drag = 0; // or some other small value
            if (playerRB.velocity.sqrMagnitude > Mathf.Epsilon)
            {
                playerRB.velocity = player.transform.TransformDirection(movement.normalized) * playerRB.velocity.magnitude;
            }

            playerRB.AddRelativeForce(movement * moveForce * Time.deltaTime);
        }
        else
        {
            playerRB.drag = 100f; // or some other value larger than the small one
        }
    }

现在,我们正在检查是否要应用不可忽略的运动量(基本上为0),如果要进行运动,则不施加阻力,因为那样会减慢正加速度这样一来,很难预测设置游戏时应具有的值。

由于我们现在没有在物体上的阻力,因此它应该根据物理原理漂移。这就是您目前所看到的。如果我们不希望这样做,可以检查是否有不可忽略的速度(基本上为0),并将其与所需的运动方向对齐。不要忘记那movement仍然在局部空间,而playerRB.velocity在世界空间。

然后,由于我们有要应用的运动,因此应该将我们的局部运动应用到我们的身体,AddRelativeVelocity以继续沿所需方向加速。目前,这种情况一直持续下去,但是您最终可能希望限制最大速度。

如果我们不应用运动,那么我们希望物理学应用阻力并降低玩家的速度。如果我们让物理学做功,您应该获得合理的加速或减速速度(取决于阻力值和作用力),但是由于我们忽略了漂移,因此您应该立即转向。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Transform.up works, -transform.up after 1 second does not work well? C# Unity

来自分类Dev

Unity3D C#。在保持两个对象之间的距离恒定的同时使用transform.RotateAround()函数

来自分类Dev

Unity3D C#。在保持两个对象之间的距离恒定的同时使用transform.RotateAround()函数

来自分类Dev

根据Unity(C#)中的地形旋转对象

来自分类Dev

在圆阵中生成的对象的Shift旋转-Unity C#

来自分类Dev

找不到Unity C#图像对象

来自分类Dev

找不到Unity C#图像对象

来自分类Dev

unity C# 对象实例 = null

来自分类Dev

在Unity C#中绕中心点旋转

来自分类Dev

移动时如何忽略旋转?(在Unity中使用C#)

来自分类Dev

Unity脚本(C#)限制相机在y轴上的旋转

来自分类Dev

Unity C#/Blender 模型旋转不正确

来自分类Dev

C#/ Unity-用于位置和旋转的TCP / UDP Multyplayer服务器更新?

来自分类Dev

Unity C# 文本不会改变

来自分类Dev

Unity C#对象引用未设置为对象的实例

来自分类Dev

从Parse.com C#(Unity)获取所有对象

来自分类Dev

Unity / C#查找对象并获取组件

来自分类Dev

如何在Unity中使游戏对象指向鼠标?(C#)

来自分类Dev

Unity3D C#对象移动

来自分类Dev

在Unity C#上拖动时,对象会变大

来自分类Dev

使用DontDestroyOnLoad Unity C#在场景之间保存对象

来自分类Dev

如何在Unity中使游戏对象指向鼠标?(C#)

来自分类Dev

使用DontDestroyOnLoad Unity C#在场景之间保存对象

来自分类Dev

Unity Animations C#

来自分类Dev

Unity C# 脚本,Vector3 未更新

来自分类Dev

Unity3D C# 私有变量不会使用公共 setter 函数更新

来自分类Dev

Unity/C# - 基于另一个对象手动设置对象的位置和旋转

来自分类Dev

Transform.up可以正常工作,一秒钟后-transform.up不能正常工作吗?C#Unity

来自分类Dev

在unity3d C#中获取下一个同级gameobject(transform)方法

Related 相关文章

  1. 1

    Transform.up works, -transform.up after 1 second does not work well? C# Unity

  2. 2

    Unity3D C#。在保持两个对象之间的距离恒定的同时使用transform.RotateAround()函数

  3. 3

    Unity3D C#。在保持两个对象之间的距离恒定的同时使用transform.RotateAround()函数

  4. 4

    根据Unity(C#)中的地形旋转对象

  5. 5

    在圆阵中生成的对象的Shift旋转-Unity C#

  6. 6

    找不到Unity C#图像对象

  7. 7

    找不到Unity C#图像对象

  8. 8

    unity C# 对象实例 = null

  9. 9

    在Unity C#中绕中心点旋转

  10. 10

    移动时如何忽略旋转?(在Unity中使用C#)

  11. 11

    Unity脚本(C#)限制相机在y轴上的旋转

  12. 12

    Unity C#/Blender 模型旋转不正确

  13. 13

    C#/ Unity-用于位置和旋转的TCP / UDP Multyplayer服务器更新?

  14. 14

    Unity C# 文本不会改变

  15. 15

    Unity C#对象引用未设置为对象的实例

  16. 16

    从Parse.com C#(Unity)获取所有对象

  17. 17

    Unity / C#查找对象并获取组件

  18. 18

    如何在Unity中使游戏对象指向鼠标?(C#)

  19. 19

    Unity3D C#对象移动

  20. 20

    在Unity C#上拖动时,对象会变大

  21. 21

    使用DontDestroyOnLoad Unity C#在场景之间保存对象

  22. 22

    如何在Unity中使游戏对象指向鼠标?(C#)

  23. 23

    使用DontDestroyOnLoad Unity C#在场景之间保存对象

  24. 24

    Unity Animations C#

  25. 25

    Unity C# 脚本,Vector3 未更新

  26. 26

    Unity3D C# 私有变量不会使用公共 setter 函数更新

  27. 27

    Unity/C# - 基于另一个对象手动设置对象的位置和旋转

  28. 28

    Transform.up可以正常工作,一秒钟后-transform.up不能正常工作吗?C#Unity

  29. 29

    在unity3d C#中获取下一个同级gameobject(transform)方法

热门标签

归档