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 Ben_the_Lad · Jan 19, 2014 at 10:09 PM · physicsaccelerationvehiclemomentum

Acceleration and Inertia in Top-Down 2D Game (C#)

Hey everybody!

I'm working on a prototype of a player controller that's more or less a guy on a bicycle. The game is built in 3D but is for all intents and purposes a top-down 2D driving game. I'm representing the player with a sprite with a box collider

My requirements for the bike are simple, but are proving difficult for me to implement 'cause I'm neither the best physics guy nor the best coder. I'd like help figuring out how to get rolling with these bullet points:

  1. 8-Directional (or even analog) Movement

  2. Acceleration and building momentum while a directional button is being held down (When the biker is pedalling)

  3. A maximum speed at which the bike "tops out" and ceases to accelerate

  4. Deceleration/drag and eventual stop while no directional buttons are being held down (when the biker stops pedalling)

I've got something that kinda works here:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
     // Exposed to Editor for Easy manipulation
     public float moveSpeed;            // player's base movement speed
 
     // START - Use this for initialization
     void Start ()
     {
         // Nothing yet!
     }
     
     // UPDATE is called once per frame
     void Update ()
     {
         // Kinda Works:
         rigidbody.AddForce((Input.GetAxis("Horizontal")), 0,(Input.GetAxis("Vertical")),ForceMode.VelocityChange);
     }
 }


This gives me a bit of acceleration and resistance, which is nice. However, there are some problems with this code:

  1. No control over rate of acceleration (I need this exposed to the editor so I can easily tweak it)

  2. Almost immediate stop (no decelerating drift when fingers are off the keys -- also ideally exposed to editor)

  3. Using two keys (say Up and Left) at once gives me double speed when moving diagonally.

And I've tried modifying this, replacing it, and jury-rigging it with no real success. I've also tried toying with Unity's built-in stuff for acceleration, but it's eluding me.

Can anyone explain how to get this working? My most strenuous requirement here is that I need to understand what I'm doing pretty well so I can modify it moving forward with the project.

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 cpm32 · Oct 12, 2015 at 04:16 PM

For anyone after a copy/paste script to drop into a 5.1 character to make it move like Mario, here you go. There is currently no slow down but that's not too hard to add in.

 using UnityEngine;
 using System.Collections;
 
 public class Bug2 : MonoBehaviour {
 
     public float moveForce;         
     public float maxSpeed;     
 
         private Vector3 v;
         
         void Update () {
             v = new Vector3(Input.GetAxis("Horizontal"), 
                     Input.GetAxis("Vertical"), 0.0f);
         }
         
         void FixedUpdate() {
             GetComponent<Rigidbody2D>().velocity = Vector3.ClampMagnitude 
             (GetComponent<Rigidbody2D>().velocity, maxSpeed);
             GetComponent<Rigidbody2D>().AddForce(v.normalized * moveForce);    
         }
 }




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
0

Answer by robertbu · Jan 20, 2014 at 12:28 AM

  1. You need to multiply the vector by your moveSpeed in order to use moveSpeed.

  2. The immediate stop is likely because you've modified 'Drag' in your Rigidbody component. If so, set this back to 0.0 to start.

  3. You can normalize the vector you use for moving to solve this issue.


    using UnityEngine; using System.Collections;

    public class Bug2 : MonoBehaviour {

      // Exposed to Editor for Easy manipulation
         public float moveSpeed = 1.0f;         // player's base movement speed
     
         void Update () {
             Vector3 v = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
             rigidbody.AddForce(v.normalized * moveSpeed,ForceMode.VelocityChange);
         }
     }
    
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 Ben_the_Lad · Jan 20, 2014 at 03:47 AM 0
Share

Thanks for your answer, robertbu! It was helpful: Normalizing the vector works for my diagonal problem. I've also adjusted rigidbody.drag and gotten some favorable results.

I'm still not getting anywhere with acceleration, however. As a hack, I've tried incrementing drag in the update function based on button down ( -= if a button is down, += if it's not) to give the vehicle smooth acceleration and deceleration by changing its drag, but it's not really working, likely because update happens way too fast to be noticeable.

I want the bike to kind of chug to get up to speed, and drift when you cease providing it input, kind of like the original Sonic the Hedgehog, if he were running on a flat surface. Takes a second to get going, then goes nice and fast, and slows to a stop only after you've had your finger off the Dpad for a few moments.

Any suggestions on how to get an editor-exposed acceleration variable that I can use to get up to max speed?

avatar image HappyMoo Ben_the_Lad · Jan 20, 2014 at 03:55 AM 0
Share

Please don't post comments as answers. Check out the Tutorial Video

avatar image robertbu · Jan 20, 2014 at 04:40 AM 0
Share

Here is a bit of an extension to the previous script. Set Rigidbody.drag to 0.0. Play with moveForce to see if you can't get what you want. 'maxSpeed' will limit the maximum speed the object can obtain.

 using UnityEngine;
 using System.Collections;
 
 public class Bug2 : $$anonymous$$onoBehaviour {
 
     public float moveForce = 2.0f;        
     public float maxSpeed = 4.5f;
 
     private Vector3 v;
 
     void Update () {
         v = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
     }
 
     void FixedUpdate() {
         rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude (rigidbody.velocity, maxSpeed);
         rigidbody.AddForce(v.normalized * moveForce);
 
     }
 }
avatar image cpm32 robertbu · Oct 12, 2015 at 03:30 PM 0
Share

Such a great script! I love it.

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

21 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

Related Questions

add acceleraction to ship controls 0 Answers

ball loses momentum 4 Answers

Vehicle - terrain collision design 1 Answer

How can I remove momentum/inertia? 0 Answers

Simulating Fighter Jet Physics (with addForce () ) 2 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