简体   繁体   English

Unity:RigidBody2D,如何以直线移动游戏对象(无曲线)

[英]Unity: RigidBody2D, how to move GameObject in straight lines (no curves)

直线运动统一

Hello all, I'm very new to and very confused by Unity.大家好,我对 Unity 很陌生,也很困惑。

I'm creating a top-down, gravity-free test game, similar to PacMan (ie think of the movement of characters in straight lines).我正在创建一个自上而下、无重力的测试游戏,类似于 PacMan(即考虑角色在直线上的移动)。

Currently, I'm trying to move an object in straight lines (without curvature) (see image).目前,我正在尝试以直线(无曲率)移动 object(见图)。

My GameObject has RigidBody2D attached, as well as Linear , Angular and Gravity set to 0.我的游戏对象附加了RigidBody2D ,以及LinearAngularGravity设置为 0。

I'm currently controlling my GameObject by adding force:我目前正在通过增加力来控制我的游戏对象:

private Rigidbody2D _playerRB2D;
public float _speed;

private void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector2 movement = new Vector2(moveHorizontal, moveVertical);

    _playerRB2D.AddForce(movement * _speed);
}

The idea is that the game object won't get to slow down, so is constantly in motion (in straight lines).这个想法是游戏 object不会减速,所以一直在运动(直线)。 Collision with objects is obviously important too!与物体的碰撞显然也很重要!

Any help or information to look at will help a lot, thank you!任何要查看的帮助或信息都会有很大帮助,谢谢!

The idea is basically to reset the force being applied on the rigidBody2D on each changed direction.这个想法基本上是重置在每个改变方向上施加在rigidBody2D上的力。 In fact, you would have to test if the direction input from horizontal is greater than vertical for x axis, and the inverse for y axis, and thus if it is true, change the opposite to 0f.实际上,您必须测试从水平方向输入的方向是否大于 x 轴的垂直方向,以及 y 轴的逆方向,因此如果为真,则将相反的方向更改为 0f。 You would have normally:你通常会:

Vector2 movement = new Vector2(moveHorizontal < moveVertical ? moveHorizontal: 0f, moveVertical < moveHorizontal ? moveVertical: 0f);
_playerRB2D.velocity = movement * speed;

Edit: By testing the input value, you can move your RigidBody2D with a analogic controller, and thus being straight as you wished !编辑:通过测试输入值,您可以使用模拟 controller 移动 RigidBody2D,从而如您所愿!

Unity has a built-in physics engine that calculates movement based on velocity (and collisions, etc.) By using Rigidbody2D.AddForce, you are adding to your rigidbody's velocity. Unity 有一个内置的物理引擎,可以根据速度(和碰撞等)计算运动。通过使用 Rigidbody2D.AddForce,您可以增加刚体的速度。 This means that if you press the up arrow and then the right arrow, the velocity from pressing the up arrow remains, causing a diagonal velocity and curved path.这意味着,如果您按向上箭头,然后按向右箭头,则按向上箭头的速度保持不变,从而导致对角速度和弯曲路径。 If you want straight lines, you can use Rigidbody2D.velocity or just use Transform.translate.如果你想要直线,你可以使用 Rigidbody2D.velocity或者只使用 Transform.translate。

_playerRB2D.velocity = movement * _speed;

Edit: You said that collisions were important, so keep the rigidbody编辑:你说碰撞很重要,所以保持刚体

or remove the rigidbody2D component and use或删除刚体2D组件并使用

transform.Translate(movement * _speed);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM