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 RadicalM1nd · Jul 16, 2014 at 06:58 PM · movementrigidbody2dmomentum

Issues with my 2D movement script & rigidbody2D

Hi guys,

first off, I will post my script. It uses rigidbody2D.AddForce to move the character. I also have a GUI Element that shows me the current Speed. However, there are some problems. First off, the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed = 100f;
     public float maxMoveSpeed = 15f;
     private float currentMoveSpeed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.LeftArrow)) {
                         rigidbody2D.AddForce (Vector3.left * moveSpeed);
                         transform.localScale = new Vector3 (-2, 2, 2);
 
                 }
 
 
         if (Input.GetKey (KeyCode.RightArrow)) {
             rigidbody2D.AddForce(Vector3.right * moveSpeed);   
             transform.localScale = new Vector3 (2, 2, 2);
             
 
 
         }
 
     if (rigidbody2D.velocity.magnitude > maxMoveSpeed) {
                         rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxMoveSpeed;
                 }
 
         currentMoveSpeed = rigidbody2D.velocity.magnitude;
     }
     
 
     void OnGUI() {
                 GUI.Label (new Rect (50, 25, 200, 30), "Current Move Speed:"+currentMoveSpeed.ToString ());  
         }
 
 
 }

  1. My first problem is the movespeed and maxmovespeed. Generally this works, but it takes way too long for the character to reach the max speed. That's why my moveSpeed is set so super high. However, when I just tap the key for 1 frame the movementSpeed goes higher than the maxMoveSpeed. What can I do to get a quick acceleration to my desired speed without having this issue?

  2. Related to the first point, but also an individual problem: When I let go of the button, my character slides really, really far. If this is not completely related to one, what do i do do quickly decrease the momentum as to not have too much sliding?

Note that I have little C# experience and wrote all of this myself using the Coding API. So if there's any general improvements to be made too, it would also be good to know. I'm still learning :)

Comment
Add comment · Show 1
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 RadicalM1nd · Jul 17, 2014 at 02:31 PM 0
Share

still can't figure it out myself :/

1 Reply

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

Answer by robertbu · Jul 17, 2014 at 03:38 PM

For #2, select your object in the Hierarchy, then in the inspector, increase the 'linear drag'. Note may also have to further increase 'moveSpeed' to compensate.

You can also do a pseudo-drag this way (executed in FixedUpdate()):

  rigidbody.velocity = rigidbody.velocity * 0.95f;

It behaves a bit differently than the 'drag' setting, and adjust the 0.95f to fit your situation.

You need to change Update() to FixedUpdate() in your code. The frame rate of Update() varies based on load, and based on the device, so you ship will have varying behaviors if you use Update(). Note that you do not want to read single key input in Update() like GetKeyDown(). Since FixedUpdate() and Update() run at different frame rates, you can lose input if you use GetKeyDown() in FixedUpdate(). But continuous input like GetKey() is generally okay.

Your 'moveSpeed' is misnamed. It would be better named 'moveForce' since it is only indirectly related to speed. How much force you need to apply to get a perceived movement is a factor of the mass of the object and the scale of your scene. So if you need it to come up to speed faster, just make the number bigger.

when I just tap the key for 1 frame the movementSpeed goes higher than the maxMoveSpeed

I see nothing in the code that would account for this. Any chance you have some sort of collision going on? Put an OnCollisionEnter2D() callback on your object to check.

Note for future questions, on Unity Answers, we like to see single, specific questions. Multiple questions are harder to answer and you end up with situations like this one where I hand some but not all the answers. Often multi-question posts are rejected or closed.

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 RadicalM1nd · Jul 17, 2014 at 03:51 PM 0
Share

Oh, sorry, I didn't know about the multiple/individual question thing. I will keep that in $$anonymous$$d for the future.

Your answer worked perfectly fine. I probably just thought the movespeed went higher because i immediately "flung" the character in one direction and the sliding was faster than the actual walking.

Thanks for clearing up the issue with the Update/FixedUpdate too, i did not know about 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

2 People are following this question.

avatar image avatar image

Related Questions

Carry momentum from a move 0 Answers

How to go about having two seperate Game Objects follow eachother? 1 Answer

2D Character suddenly stops moving for no given reason 1 Answer

Simple Rigidbody2D movement and jump 0 Answers

2D "Platformer" Movement Problems 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