简体   繁体   English

与编辑器 Unity 2D 相比,玩家在构建中的移动速度更快

[英]Player moves way faster in build compared to editor Unity 2D

My question is the same as the title.我的问题和标题一样。 My player and enemies both move WAY faster when I build the game compared to when I play in the editor.与我在编辑器中玩游戏时相比,当我构建游戏时,我的玩家和敌人的移动速度都快得多。 I have looked online, but most forum pages say to multiply movement by Time.deltaTime, but for some reason, this doesn't work for me, because even when I re-build the game movement is way faster.我在网上看过,但大多数论坛页面都说将运动乘以 Time.deltaTime,但由于某种原因,这对我不起作用,因为即使我重新构建游戏运动也更快。 If it helps, my character and enemies are both moved by Rigidbody2D.MovePosition.如果有帮助,我的角色和敌人都会被 Rigidbody2D.MovePosition 移动。 I think that I need to limit the framerate so that everything is slower, but I'm not sure.我认为我需要限制帧速率,以便一切都变慢,但我不确定。 If you think I should, how would I go about doing that?如果您认为我应该这样做,我会怎么做? Thanks in advance.提前致谢。

Both the enemies and player use this to move:敌人和玩家都使用它来移动:

GetComponent<Rigidbody2D>().MovePosition(transform.position + move * speed * Time.deltaTime);

If it helps, "move" is a Vector3 coordinate and speed is set to 30 for the player character.如果有帮助,“移动”是 Vector3 坐标,玩家角色的速度设置为 30。 I have clicked "build and run" every time after I made a change.每次进行更改后,我都会单击“构建并运行”。

Is your code in FixedUpdate() or Update()?您的代码是在 FixedUpdate() 还是 Update() 中? Physics code should be in FixedUpdate().物理代码应该在 FixedUpdate() 中。 Note I changed Time.deltaTime to Time.fixedDeltaTime注意我将 Time.deltaTime 更改为 Time.fixedDeltaTime

void Update() {
  //Your Non Physics Code
}

void FixedUpdate() {
   //Your Physics code aka
   GetComponent<Rigidbody2D>().MovePosition(transform.position + move * speed * Time.fixedDeltaTime);

}

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

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