Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 EZhurst · Oct 13, 2014 at 09:49 PM · movementphysicsphysics2d

Physics2D AddForce problems

I'm using AddForce to move my player, and when I run it in the editor, it works great. However, if I build and run it, the forces added seem to be less than when playing in the editor, meaning the player moves a lot slower. It is even worse when running in editor with the animator tab open(much slower than in a build). I have no idea why this is, and cannot seem to figure it out. The script I'm using is below. Thanks for any help!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMove : MonoBehaviour
 {
     public float moveSpeed = 1f;
     public float jumpHeight = 3f;
     public float thrusterPower = 1f;
     public Transform lineStart, lineEnd;
     public bool isGrounded;
     public bool thrusterEnabled;
 
     void Update ()
     {
         if(Input.GetKey(KeyCode.D))
         {
             rigidbody2D.AddForce(Vector2.right * moveSpeed);
         }
 
         if(Input.GetKey(KeyCode.A))
         {
             rigidbody2D.AddForce(-Vector2.right * moveSpeed);
         }
 
         if(Input.GetKey(KeyCode.Space))
         {
             rigidbody2D.AddForce (Vector2.up * thrusterPower);
             thrusterEnabled = true;
         }
         else
         {
             thrusterEnabled = false;
         }
 
         if(Physics2D.Linecast(lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer("Floor")))
         {
             isGrounded = true;
         }
         else
         {
             isGrounded = false;
         }
 
         if(isGrounded == true)
         {
             if(Input.GetKeyDown(KeyCode.W))
             {
                 rigidbody2D.AddForce (Vector2.up * jumpHeight * 100);
             }
         }
     }
 }

Also, I have a very powerful computer and checked the processes panel and i was only running at 12% memory and 30% CPU.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Oct 13, 2014 at 09:56 PM

Your code is framerate-dependent since you are using Update, which runs once every frame and has no fixed rate. Physics should only be done in FixedUpdate. (But note that GetKeyDown should not be used in FixedUpdate, since GetKeyDown is only true for the single frame when the key is pressed, which does not necessarily coincide with a FixedUpdate frame.)

Comment
Add comment · Show 3 · 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 EZhurst · Oct 13, 2014 at 10:15 PM 0
Share

Thank you for such a quick reply. So how would I go about rewriting the code without using the update function, because Get$$anonymous$$eyDown seems to be the only way to get a continuous force acting upon the player?

avatar image Eric5h5 · Oct 13, 2014 at 10:23 PM 0
Share

Get$$anonymous$$eyDown is a one-off; Get$$anonymous$$ey is continuous. You can put Get$$anonymous$$ey in FixedUpdate, and use Get$$anonymous$$eyDown in Update to trigger a bool which is checked in FixedUpdate.

avatar image EZhurst · Oct 13, 2014 at 11:59 PM 0
Share

Yeah, haha I don't know what I was thinking when I commented, I mixed up Get$$anonymous$$eyDown with Get$$anonymous$$ey. Thank you very much.

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

29 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

Related Questions

Need help with my movement script.. :( 1 Answer

Is there a standardized way for creating a 2D ball physics movement method? 0 Answers

Moving a non-player object in a random direction which bounces off other objects. 2 Answers

2D Smooth movement and collision detection problem 0 Answers

Translate to a target goal position and facing direction and stop 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