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
1
Question by BarelyUsedDesert · Jan 05, 2021 at 03:33 PM · 2dscripting problemunity 2dupdateaddforce

AddForce works in Editor but not in build

I'm currently creating a game similiar to Lunar Lander, but i'm currently stuck on a weird problem. Rigidbody.AddForce completely works in the Editor, but not in the final build/during runtime. Here's my affected script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Controller : MonoBehaviour
 {
     public GameObject rocket;
     public ParticleSystem particles;
 
     public Rigidbody2D rbRocket;
 
     public float thrust = 1.0f;
     public float mass;
     public static float fuel = 100;
     public float loss = 1;
 
     public static bool SAon = false;
     public bool wastingFuel = false;
 
     // Start is called before the first frame update
     void Start()
     {
         particles = GetComponent<ParticleSystem>();
         rbRocket = rocket.GetComponent<Rigidbody2D>();
         particles.Stop();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (fuel <= 0)
         {
             return;
         }
 
         if (Input.GetKey(KeyCode.W))
         {
             rbRocket.AddForce(Time.timeScale * thrust * transform.up);
         }
 
         float horizontalKeys = Input.GetAxis("Horizontal");
 
         rbRocket.AddTorque(-horizontalKeys * (rbRocket.mass / 4));
 
         if (Input.GetKey(KeyCode.W))
         {
             fuel -= Time.deltaTime * loss;
             particles.Play();
 
         }
         if (Input.GetKeyUp(KeyCode.W))
         {
             particles.Stop();
         }
 
         if (SAon)
         {
             rbRocket.angularDrag = 5;
         }
         else
         {
             rbRocket.angularDrag = 1;
         }
 
         if (Pause.isPaused == true)
         {
 
         }
     }
 
     void FuelGone()
     {
 
     }
 }
 

(The player flies using AddForce in the editor, but doesn't in the final build)

NOTE: I'm not able to use FixedUpdate() since that makes that problem also occur in the editor.

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 Pangamini · Jul 18, 2021 at 02:36 PM

AddForce adds impulse and therefore speed over time. Specifically, it uses the fixedDeltaTime, in the FixedUpdate. In general, you should be applying forces and impulses to any rigidbodies in the FixedUpdate instead of Update, as it's synchronized with it (Update and FixedUpdate are not synchronized, and one can be called more times than the another). AddForce wikth default Force mode is also already applied 'over time', which means you should not multiply the force by deltaTime . If you want to do that for some reason, use ForceMode.Impulse instead.

[1]: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

Comment
Add comment · 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
0

Answer by jroussos01 · Jul 17, 2021 at 10:26 PM

It looks to me like your problem is that the way you are adding force is framerate dependent, since you are calling addForce in Update(). I would try multiplying your thrust variable by Time.deltaTime, and it should hopefully fix your problem. I had a very similar scenario as yours and found that it worked, but you may have to increase your thrust quite significantly. For example:

 public float thrust = 500.0f

 if (Input.GetKey(KeyCode.W))
          {
              rbRocket.AddForce(Time.timeScale * thrust  * Time.deltaTime * transform.up);
          }
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 Eno-Khaon · Jul 18, 2021 at 06:19 PM 0
Share

As @Panga$$anonymous$$i says, you don't want to multiply your framerate-independent force by Time.deltaTime (regardless of which function it's in - In FixedUpdate(), it's automatically converted to the same value as Time.fixedDeltaTime). This ADDS framerate-dependence to your script.

Specifically, your example (assu$$anonymous$$g Vertical Sync of 60 frames per second) multiplies by approximately 8 1/3. 500/60 = 8.333~

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

331 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 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

Unable to make 2D enemy zigzag : Top Down View 1 Answer

How do I manage multiple colliders on a gameObject? 1 Answer

Mouse Click Walk to Idle Animations 0 Answers

Teleport Not Working in Unity 2D 1 Answer

How to gradually increase Obstacle holders speed over a certain period 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