Reputation: 57
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. If it helps, my character and enemies are both moved by 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. I have clicked "build and run" every time after I made a change.
Upvotes: 2
Views: 2484
Reputation: 914
Is your code in FixedUpdate() or Update()? Physics code should be in FixedUpdate(). Note I changed Time.deltaTime to Time.fixedDeltaTime
void Update() {
//Your Non Physics Code
}
void FixedUpdate() {
//Your Physics code aka
GetComponent<Rigidbody2D>().MovePosition(transform.position + move * speed * Time.fixedDeltaTime);
}
Upvotes: 5