简体   繁体   English

Unity C# Player 使用 Rigidbody2D.AddForce 速度太快

[英]Unity C# Player going too fast with Rigidbody2D.AddForce

So I'm trying to make a game in Unity 5 .所以我正在尝试在Unity 5 中制作游戏。 It's a 2D game and I want my character to automatically go forward.这是一个2D 游戏,我希望我的角色自动前进。 But my problem is that since I use Rigidbody2D.AddForce , it keeps adding force every frame and I don't want that.但我的问题是,由于我使用Rigidbody2D.AddForce ,它会不断增加每一帧的力,我不想要那样。 I want my speed to be at 5.0f , not more or less.我希望我的速度5.0f ,而不是更多或更少。 Is there a way to set a limit or keep a constant speed?有没有办法设置限制或保持恒定速度?

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

public Rigidbody2D player;

public bool grounded = false;
private bool hasJumped = false;

public float movementspeed = 5.0f;
public float jumpforce = 450.0f;

// Use this for initialization
void Start () {
    player = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {

    player.AddForce(transform.right * movementspeed);

    // (JumpScript)
}

void FixedUpdate(){

    // (JumpScript)

}
}

I am very suprised at the answers here, they are way off.我对这里的答案感到非常惊讶,它们离我们很远。 Your problem is that you are adding force in the update, which is called every frame.你的问题是你在更新中添加了力量,每帧都被调用。 That means that every frame of the game the object will gain 5 more force and speed up infinitely.这意味着游戏的每一帧对象都会获得 5 倍以上的力并无限加速。

If you want the object to get 5 force and then stay at that speed, just put the add force in the start instead of the update.如果您希望对象获得 5 个力,然后保持该速度,只需在开始时添加力而不是更新。 Like this:像这样:

// Use this for initialization
void Start () {
    player = GetComponent<Rigidbody2D>();
    player.AddForce(transform.right * movementspeed);
}

// Update is called once per frame
void Update () {

    // (JumpScript)
}

You could just set the Rigidbody.velocity to 5.您可以将 Rigidbody.velocity 设置为 5。

However, sooner or later knowing some physics will come very handy.但是,迟早知道一些物理学会非常有用。 The absolute minimum is knowing Newton's laws.绝对最低限度是了解牛顿定律。

In your case, if you are absolutely dedicated to using AddForce() you could use ForceMode.Impulse as the 3rd parameter to give your object an instant push (instead of gradual acceleration).在您的情况下,如果您绝对致力于使用AddForce()您可以使用ForceMode.Impulse作为第三个参数,为您的对象提供即时推动(而不是逐渐加速)。 This way you would have to call AddForce() only once, not repeatedly.这样你就只需要调用AddForce()一次,而不是重复调用。 You can calculate the exact amount of force you need by the formula for Kinetic energy:您可以通过动能公式计算出您需要的确切力量:

Force = 0.5f * Mass * Velocity^2力 = 0.5f * 质量 * 速度^2

I'm writing a 2D game like yours in unity too.我也在写一个像你一样的 2D 游戏。 And I use an if to check the velocity and use another variable for the force like this:我使用if来检查速度并使用另一个变量来表示这样的力:

public float acel = 50, speed = 5;
Rigidbody2D rbody;
void FixedUpdate () {
    // As you want to move right, we don't have to calculate the absolute value
    if (rbody.velocity.x < speed)
        rbody.AddForce(Vector2.right*acel);
}

The speed won't always be 5 but maybe that isn't important though.速度并不总是 5,但也许这并不重要。

If you want to use Physics to move your player at a constant speed then you need to apply force at the beginning until the speed you want is reached, only then change force to 0. In order for this to work as expected I would also add a Physic Material to the collider of the character to make sure that no friction is slowing down the character.如果您想使用 Physics 以恒定速度移动您的播放器,那么您需要在开始时施加力,直到达到您想要的速度,然后才将力更改为 0。为了使其按预期工作,我还要添加为角色的碰撞器添加物理材质,以确保没有摩擦减慢角色的速度。

PS: If you are creating some sort of infinite runner game I would consider moving and reusing the environment rather than the player. PS:如果您正在创建某种无限跑酷游戏,我会考虑移动和重用环境而不是玩家。

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

相关问题 unity RigidBody2D.AddForce() 方法停止玩家移动,卡住不动 - Unity RigidBody2D.AddForce() method stopped player movement, stuck without move 刚体2d.movePosition 和刚体2d.AddForce 之间的交互是如何工作的? - How does the interaction between rigidbody2d.movePosition and rigidbody2d.AddForce works? “oncollisionenter”中的 Rigidbody.Addforce 未在统一 c# 中执行 - Rigidbody.Addforce in"oncollisionenter" is not executing in unity c# Unity C#-Rigidbody.AddForce无法按预期工作 - Unity C# - Rigidbody.AddForce not working as intended 具有 ForceMode2D.Impulse 的 Rigidbody2D.AddForce 仅在初始触发输入后对 Y 轴施加力 - Rigidbody2D.AddForce with ForceMode2D.Impulse only applies force to the Y-axis after initial trigger entry 我角色的水平运动阻止我使用 rigidBody2D.AddForce(Vector2.right) 或任何水平轴 - My character's horizontal movement is stopping me from using rigidBody2D.AddForce(Vector2.right) or any horizontal axis Unity C#检查播放器(Rigidbody2D)是否停止移动了x秒 - Unity C# check if player (Rigidbody2D) stopped moving for x seconds rb.AddForce 但对于 2D 怎么办? 统一C# - How to do rb.AddForce but for 2D? Unity C# 刚体在 Unity 中不断移动 (AddForce) - Rigidbody keeps moving in Unity (AddForce) 如果设置C#计时器会过快 - c# timer is going too fast if set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM