AddTorque() working differently than inside Editor

I finally finished my snowboarder game, and I tricked it out like CRAZY! I have a 7 level incredibly difficult rage game where you have to dodge all kinds of flying objects and projectiles, and I was incredibly happy to finally complete it. I created a build, but when I opened it up, I was incredibly disheartened to discover that my movement was nothing like inside the editor. This was some weird buggy thing I had encountered, but it always fixed itself when I clicked on the Editor screen. What happened? The game is unplayable without a fix… so how can I fix things?
Thank you!

Hi GameTortoise,

It’s great to read that this game inspired you to enhance it and to add more ideas to make it more exciting. :slight_smile:

The problem with the game window is that it is a laggy preview only. For this reason, little differences between the game window and the build are usually expected. Always fine-tune your game for the build, not for the game window.

Another problem is the physics simulation. In this course, Rick wanted to keep things simple because most of his students are beginners who don’t want to be overwhelmed with ‘too much information’. However, since you already invested so much time into your game, I’m telling you how to make the player be moved properly with the physics simulation.

Four important things you need to know first:

  • The Rigidbody2D is part of the physics simulation.
  • The code for the physics simulation needs to be in the FixedUpdate method, thus the Rigidbody2D code needs to be in the FixedUpdate method.
  • Time.deltaTime must not be used in FixedUpdate.
  • User input should be processed in Update, not FixedUpdate because many user input events are frame-based. While Update gets executed each frame, FixedUpdate gets executed every x milliseconds. In the worst case, FixedUpdate misses user input.

If you understood this, the solution is fairly straightforward. If you want to challenge yourself, you could try to apply what you’ve just learnt.

If you just want to see a solution or compare yours to mine, expand the solution. :)
  1. Make a backup of your player code, so you’ll be able to retrieve it if needed.
  2. Add the FixedUpdate method.
  3. Move the code that uses the Rigidbody2D to the FixedUpdate method.
  4. Remove Time.deltaTime if you see it in FixedUpdate().
  5. Create a variable at the top of your class where you assign the values from the user input.
  6. Use that variable in the AddTorque method call (which should be in the FixedUpdate method).
  7. In Update, get the user input, and assign the values to the variable you created in (5).
  8. Save your script.

Theoretically, this should fix certain problems with the movement and rotation in the build. The game window will always be a little laggy, so test the build to figure out if the problem is in your game or just in the Unity Editor.

I hope this helped. :slight_smile:


See also:

Thank you so much!!! I’ll fix it now(hopefully)

image

IT WORKS EXCELLENTLY! Thank you so much, I was getting really worried for a minute there.

Good job! I’m glad the problem is solved. :slight_smile:

If you don’t mind, I’m leaving a little tip for you that allows you to simplify your code a bit: As you probably noticed yourself, there are 2 code blocks in the RotatePlayer method that are basically duplicates of the first if-block. Of course, they are not identical, but it wouldn’t it be great if you could use only 2 lines instead of 6?

The solution is very simple, so I’m leaving a hint first in case you want to improve your coding skills: An if-condition is a boolean expression. It gets evaluated to either true or false.

And here is a suggestion for a simplified solution:

You need two lines only, and no if-statements:

turningLeft = Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A);
turningRight = Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D);

That’s all. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms