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 djfluffwug · Oct 07, 2011 at 03:44 PM · rigidbodyaddforcespeedballacceleration

Ball Addforce Acceleration Speed

Hello, I am not too good with scripting in Unity and I have come across a few problems. I have tried for weeks to get it working but with no success.

Basically, I am trying to make a game where the player has to navigate a level using a wooden ball. They use the arrow keys to controll the rolling of the ball.

I have made a script using AddForce with Rigidbodies on my ball object but the ball rolls incredibly slow and takes ages to change direction.

I want to be able to have a fast acceleration and be able to change directions fast but still have a relatively low top speed.

Here is my script so far:

 function FixedUpdate () {
 
     if (Input.GetKey ("up")) 
         {
             rigidbody.AddForce (Vector3.forward * Time.deltaTime * 5, ForceMode.Acceleration);
         }
     
     
     if (Input.GetKey ("down")) 
         {
             rigidbody.AddForce (Vector3.forward * Time.deltaTime * 5, ForceMode.Acceleration);
         }
     
     
     if (Input.GetKey ("left")) 
         {
             rigidbody.AddForce (Vector3.left * Time.deltaTime * 5, ForceMode.Acceleration);
         }
         
         
 if (Input.GetKey ("right")) 
     {
         rigidbody.AddForce (Vector3.right * Time.deltaTime * 5, ForceMode.Acceleration);
     }


I really need help with getting this script to work how I have described. Any help would be greatly appreciated.

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
2
Best Answer

Answer by syclamoth · Oct 07, 2011 at 04:02 PM

You're on the right track, except for some specifics-

  1. If you use ForceMode.Acceleration, there will be virtually no top speed. It's basically emulating gravity!
  2. There isn't really an inbuilt way to give a rigidbody a 'top speed' as such- you can, however, give it relatively high drag and low mass, and it will stop moving soon after you stop putting a force on it.
  3. Is there any reason why you need to be using discrete inputs here? Why can't you use the Input.GetAxis("Horizontal/Vertical")? If you do something like that, you could fake out your drag to give a better response time, and still control the desired velocity.

This is how I would do it:

public float desiredSpeed; public float maximumDrag; public float forceConstant; void Update() { Vector3 forceDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //This reduces drag when the player adds input, and makes it stop faster. rigidbody.drag = Mathf.Lerp(maximumDrag, 0, forceDirection.magnitude); // this reduces the amount of force that acts on the object if it is already // moving at speed. float forceMultiplier = Mathf.Clamp01((desiredSpeed - rigidbody.velocity.magnitude) / desiredSpeed); // now we actually perform the push Rigidbody.AddForce(forceDirection * (forceMultiplier * Time.deltaTime * forceConstant));

}

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 djfluffwug · Oct 07, 2011 at 04:20 PM 0
Share

Thanks for the tips. I have been trying to give it a high drag and have been nearly reaching the desired results I want but then gravity is really slow with a high drag.

I am not used to the C# language so I will learn it so I can understand better. I guess I should just work on my coding a little more before getting into 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Ball physics help 2 Answers

How to reach a specific max speed with AddForce() ? 1 Answer

[Solved] Unity rigidbody.addForce ignores time.deltaTime? Possibly just a code bug I cannot find. 0 Answers

Rigidbody.AddForce 2d movement without acceleration 2 Answers

Confusion with AddForce with ForceMode.Acceleration 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