简体   繁体   English

我角色的水平运动阻止我使用 rigidBody2D.AddForce(Vector2.right) 或任何水平轴

[英]My character's horizontal movement is stopping me from using rigidBody2D.AddForce(Vector2.right) or any horizontal axis

I keep trying to Addforce to the left or right but nothing happens.我一直尝试向左或向右添加力,但没有任何反应。 This happened to me in another game before.这发生在我之前的另一场比赛中。 I just figured out what it was我才明白那是什么

rb.velocity = new Vector2(speed * moveX * Time.deltaTime, rb.velocity.y);

Here, I'm setting the velocity to 0 if I'm not moving (moveX is Input.GetAxisRaw("Horizontal") ) How do I AddForce horizontally while not having to disable my normal movement?在这里,如果我不移动,我将速度设置为 0(moveX 是Input.GetAxisRaw("Horizontal") )如何水平添加力,同时不必禁用我的正常移动? I know I can use transform.Translate but I'd prefer using rb.velocity for now.我知道我可以使用 transform.Translate,但我现在更喜欢使用 rb.velocity。

With the add force function, link to unity docs below.使用 add force function,链接到下面的统一文档。

https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

I have faced this several times but我已经遇到过好几次了,但是

  • if you are using AddForce what you can do is either make sure to add a good amount of speed value to it如果您使用的是AddForce ,您可以做的是确保为其添加大量的速度值

    rb.AddForce(inputMove.x * speed);
  • if you are using Time.deltatime or don't use time at all...something like:如果您正在使用Time.deltatime或根本不使用时间...类似:

     rb.AddForce(inputMove.x * Time.deltatime * speed);

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

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