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 Loyalgamer45 · Oct 15, 2020 at 01:15 PM · movementcharactermovement scriptspeedcharacter movement

character(ball) rolls with less speed when I build it

So I've been working on a ball roller game and it works fine till I build it. when I build it the ball rolls at a slower pace than it does in game. How do I get it for them to be the same? Code source:

 public class movement : MonoBehaviour
 {
     public Rigidbody rigid;
     public float horizontal;
     public float vertical;
     public float speed = 1f;
 
     // Start is called before the first frame update
     void Start()
     {
         rigid = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
 
         horizontal = Input.GetAxis("Horizontal");
         vertical = Input.GetAxis("Vertical");
 
         Vector3 direction = new Vector3(horizontal, 0.0f, vertical);
 
         rigid.AddForce(direction * speed);
     }
 }
 
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 Ady_M · Oct 16, 2020 at 05:17 AM

Use FixedUpdate() when dealing with Rigidbodies.
Keep in mind that you should NOT use Time.deltaTime inside FixedUpdate when calling Physics methods (Force / Torque).

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 · Oct 16, 2020 at 06:34 AM 1
Share

To add a little more detail to this, here's how AddForce() functions are utilized:

 public KeyCode actionKey = KeyCode.W;
 private bool actionTaken = false;

 void Update()
 {
     if(Input.GetKeyDown(actionKey))
     {
         actionTaken = true;
     }
 }

 void FixedUpdate()
 {
     // Apply a continuous force, X units per second acceleration
     // Factors in mass (greater mass = less push)
     // Intended to be used regularly in FixedUpdate cycle
     // Force$$anonymous$$ode.Force automatically applied when not specified
     rigid.AddForce(direction * speed * rigid.mass, Force$$anonymous$$ode.Force);

     // This applies the same force as:

     // Apply a continuous force, X units per second acceleration
     // Ignores mass (no need to multiply mass in)
     // Intended to be used regularly in FixedUpdate cycle
     rigid.AddForce(direction * speed, Force$$anonymous$$ode.Acceleration);

     if(actionTaken)
     {
         // Apply an instantaneous force of X units velocity change
         // Factors in mass (greater mass = less push)
         // Intended to be used once at a time
         // (Can generally be safely used from Update(),
         //    but FixedUpdate() is still a more reliable location)
         rigid.AddForce(direction * speed * rigid.mass, Force$$anonymous$$ode.Impulse);

         // This applies the same force as:

         // Apply an instantaneous force of X units velocity change
         // Ignores mass (no need to multiply mass in)
         // Intended to be used once at a time
         // (Can generally be safely used from Update(),
         //    but FixedUpdate() is still a more reliable location)
         rigid.AddForce(direction * speed, Force$$anonymous$$ode.VelocityChange);

         actionTaken = false; // Enforce a single physics update trigger
     }
 }


To note, the Force and Acceleration Force$$anonymous$$odes can be effectively (albeit inefficiently) converted to Impulse and VelocityChange respectively by dividing by Time.fixedDeltaTime (or, by extension, Time.deltaTime, since it returns the same value when checked in FixedUpdate()).

Conversely, Impulse and VelocityChange can be converted to Force and Acceleration by multiplying by Time.fixedDeltaTime instead.

Again, those conversions are not worth making, but the information itself can aid in understanding what the difference is between the Force$$anonymous$$ode choices.

Edit: Typo

avatar image
0

Answer by axlxi1 · Oct 15, 2020 at 03:03 PM

use time.deltatime:

 rigid.AddForce(direction * speed * Time.deltatime);

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 · Oct 16, 2020 at 06:37 AM 0
Share

As @Ady_$$anonymous$$ mentions, this is terrible advice. This decreases the provided force by 1 / Time.deltaTime times (by default, a 50x decrease) and, as a result, varies in the event that the physics timesteps are modified.

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

256 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

Related Questions

I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers

My Character is moving fine horizontally and vertically, but moves way too fast diagonally 2 Answers

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

How to make fish movement with keyboard arrows look natural? 1 Answer

Limiting Speed on Two Axis 1 Answer


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