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 PeanutButterSpice · Dec 31, 2013 at 07:38 PM · charactercontrollerrigidbody2dvelocity

Make 2D movement more precise and less based off of forces and physics

Alright, so I've been looking through all of the things Unity has on movement and 2D movement. I've looked at other people's examples, and I've looked through countless code snippets, but I cannot for the life of me find a way to make my character's movement precise.

By precise, I mean, have it stop the second you let go of a key, and begin the second you push down on a key. I don't want it to accelerate or decelerate. I want it to move at a set value constantly.

Right now, I've got it setup this way:

     void Update () 
     {
         //cache vertical input
         float vertical = Input.GetAxis("Vertical");
 
         //assign vertical input as a vector 2 value
         Vector2 movement = new Vector2 (0.0f, vertical);
 
         //use vector 2 value to add force
         rigidbody2D.AddForce (movement);
     
     }

Using this, I get movement, but it is weighty and the character does not stop moving after input is pressed because a force is still acting on the player character.

I have tried alleviating this in a number of ways.

One of these was to set up a simple set of if/else statements in order to find input, and then to assign a rigidbody2D.velocity.y directly, like below:

     float speed = 10;
     
 
     // Update is called once per frame
     void Update () 
     {
 
         if (Input.GetKey("w"))
         {
             rigidbody2D.velocity.y = speed;
         }
         else if (Input.GetKey("s"))
         {
             rigidbody2D.velocity.y = speed * -1;
         }
         else
         {
             rigidbody2D.velocity.y = 0;
         }
     
     }

This however pops up with an error in Unity asking me to store the variable in a temporary variable. And so, I have tried to do this by writing it this way:

     float speed = 10;
     
 
     // Update is called once per frame
     void Update () 
     {
         Vector2 velocity = rigidbody2D.velocity;
         rigidbody2D.velocity = velocity;
 
         if (Input.GetKey("w"))
         {
             velocity.y = speed;
         }
         else if (Input.GetKey("s"))
         {
             velocity.y = speed * -1;
         }
         else
         {
             velocity.y = 0;
         }
     
     }

However, when I do this, I try to move the character with "w" or "s" and nothing happens, it is stationary. Any help you guys could give me 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HappyMoo · Dec 31, 2013 at 09:53 PM

Input.GetAxis("Vertical") is smoothed. You can either go to the Input Manager and remove that smoothing or use Input.GetAxisRaw("Vertical").

And... you need to move this line:

 rigidbody2D.velocity = velocity;

down to after you modified velocity, so you actually change it on the rigidbody.

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 HappyMoo · Jan 03, 2014 at 10:53 PM 0
Share

Did that help?

avatar image
1

Answer by Tom Murray · Dec 31, 2013 at 10:35 PM

The physics and collision processes run in sync with FixedUpdate.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html

I was having issues with this until I changed all my Updates to FixedUpdates, and that fixed a lot of it. Also, what HappyMoo said. Move this:

 rigidbody2D.velocity = velocity;

To after the code that directly changes velocity. The way you have it now will just change rigidbody2D.velocity back to itself, which obviously wouldn't do anything.

Comment
Add comment · 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

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

20 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

Related Questions

Need help for my Arkanoid-like game. i got problem to make my ball reflects on the wall 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

2D Geometry dash-like ship physics 0 Answers

Dashing with rigidbody2D not working right 2 Answers

Move car at same speed 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