Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cristea13 · Sep 30, 2021 at 12:48 PM · editorbuildphysics2d

Physics acting different in build than in editor

Player movement script:

  void Update()
     {
 
         if (player.GetButton("start") && fire.gameState == 0)
         {
             fire.startButton();
             restart = true;
             fire.gameState = 1;
         }
         if (player.GetButton("Right"))
             right = true;
         else
             right = false;
 
         if (player.GetButton("Left"))
             left = true;
         else
             left = false;
 
         if (player.GetButton("Down"))
             down = true;
         else
             down = false;
 
         if (player.GetButton("Up"))
             up = true;
         else
             up = false;
 
 
     }
 
 
         void FixedUpdate()
     {
         if(fire.gameState == 1)
         
             if (right)
             {
                 direction = 1;
                 transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
             }
 
 
             else if (left)
             {
                 direction = 3;
                 transform.position = new Vector2(transform.position.x - speed * Time.deltaTime, transform.position.y);
             }
 
             else if (up)
             {
                 direction = 0;
                 transform.position = new Vector2(transform.position.x, transform.position.y + speed * Time.deltaTime);
             }
 
             else if (down)
             {
                 direction = 2;
                 transform.position = new Vector2(transform.position.x, transform.position.y - speed * Time.deltaTime);
             }
 
 
 
 
         }

The boulders just have a rigidbody 2D and polygon collider.

Editor mode: https://youtu.be/WR0aCkjmI-M Build mode: https://youtu.be/_cZXtBaO04I

As you can see, physics is acting different, you can easily go past the boulders in build mode but not in edit mode. Where does that come from?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KittenSnipes · Sep 30, 2021 at 12:50 PM

In your fixed update it seems you are missing a pair of curly brackets for the very first if statement. Also please make sure your code is properly tabbed for easy debugging for anyone giving answers. Is this line important: if(fire.gameState == 1) in your fixed update? If it is then it needs to be properly bracketed.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cristea13 · Sep 30, 2021 at 08:58 PM 0
Share

Thanks for pointing that out, it is a mistake, although not related to this problem.

avatar image
0

Answer by Captain_Pineapple · Sep 30, 2021 at 01:02 PM

The problem that you have here is that you multiply all your movement values by Time.deltaTime. Time.deltaTime is not needed here since you are in fixed update. Fixed update always takes place in the same timely distance. So while there always is a FixedUpdate in a interval of 0.02seconds (that is the default i think - look that up in your project physics settings) Time.deltaTime is framerate dependent. So when you build your game an Update takes less time so there are more Update calls between each FixedUpdate than in the Editor.

To keep the correct timescale i'd recommend to use Time.fixedDeltaTime here.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cristea13 · Sep 30, 2021 at 09:07 PM 0
Share

I replaced it with Time.fixedDeltaTime but there's no change in the physics behaviour.

avatar image Eno-Khaon · Sep 30, 2021 at 09:45 PM 1
Share

When Time.deltaTime is read during FixedUpdate(), it's automatically converted to/treated as Time.fixedDeltaTime. Additionally, when modifying positions directly (transform.position) to simulate movement over time, you *DO* want to make use of that scaling to make it framerate-independent.

When you want proper physics interactivity, though, transform.position will just teleport past colliders. The exact equivalent with proper physics interactions would be Rigidbody.MovePosition(), but I think there are more fundamental underlying problems here in general.

avatar image cristea13 Eno-Khaon · Sep 30, 2021 at 10:39 PM 0
Share

I don't want proper physics interactivity, though, as i am happy with how it works in the editor, and i know there are plenty of games successfully using 'transform.position' in various mechanics. The problem is the lack of consistency for these interactions between the editor and the build.

avatar image Captain_Pineapple Eno-Khaon · Oct 03, 2021 at 01:45 PM 0
Share

fair point, i forgot about this..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

184 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

HingeJoint2D physics different between editor and build (see images inside) 1 Answer

Why Unity correctly builds a project but the editor freezes on the Build Progress window? 2 Answers

Build and Editor collisions behaving different? 0 Answers

Loading screen works fine in Unity Editor, but gets stuck in the finished/built .exe version? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges