Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mcarthur03 · Jan 12, 2019 at 12:44 PM · 2dphysicsrigidbody2dplatformer

2D Physics movement system is creating unexplained random variations.

I am trying to make a movement system wherein player movement input is set to velocity to help smooth it. I am used to engines where i would usually draw to the screen and have everything stored as a variable, so unity's colliders are confusing me greatly.

  public float horizontal;
     public Rigidbody2D rb;
     public float speed;
     public float gravity;
     public float drag;
 
     public float intxvel;
     public float intyvel;
     public float extxvel;
     public float extyvel;
 
     public float xv;
     public float yv;
 
     void Start () {
         rb = gameObject.GetComponent<Rigidbody2D>();
     }
     
     void FixedUpdate () {
         xv = rb.velocity.x;
         yv = rb.velocity.y;
 
         //REFRESH
         extyvel = rb.velocity.y - intyvel;
         extxvel = rb.velocity.x - intxvel;
         extxvel *= drag;
         extyvel *= drag;
         rb.velocity = new Vector2();
 
         //PRE-PROCESS
         horizontal = Input.GetAxis("Horizontal");
         intxvel = horizontal * speed;
 
         extyvel -= 1;
 
         if (Input.GetKey(KeyCode.K)) { extxvel += 10; }
 
         //PROCESS
         rb.velocity = new Vector2(intxvel + extxvel, intyvel + extyvel);
         
         //POST-PROCESS
     }
  

This is what i have got. Ill try to explain it as best i can, starting with the distinction between intx/yvel and extx/yvel. Intxvel and intyvel are for player inputs such as the horizontal axis. the velocity perfectly matches it, and gets set to the overall velocity at the end of the code. Extxvel and Extyvel are for the actual physics velocities, such as being moved by a force. In the example provided i have gravity which subtracts one from extyvel every update.

These variables are then added together at the end, and create the velocity.

This then leads into the opening lines, where i set extyvel to the rb.velocity.y minus the intyvel. This is done so that extyvel remains the same going into the next update, yet at the same time any changes to the velocity from unities physics engine (such as hitting a wall stopping all velocity) will be taken into account and removed from the extvelocity.

xv and yv are just a way for me to view the velocity while running the game.

This little loop is something i would expect to work, i have used similar systems in past platformers i have made, however here i am noticing that i get an assortment of jittering or variation in the extvelocities where i wouldn't expect it, and only when the player is colliding with an object, which i furthermore am unable to notice in the actual game at all beyond the fact that they actively counter the intvelocities if they are not perfectly zero, and when the intvelocities are zero the extvelocities seem to change rapidly and randomly.
I can't explain why this is happening because the main velocity is equalling the extvelocities when the player is not receiving input, which should mean they stay exactly the same, and furthermore when the player moves at say an intxvel of 10, and extxvel of 0, the velocity should become 10 (which it does) and in turn extxvel become xvelocity - intxvel which is zero, not negative 10 like it returns. alt text

capture.png (9.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

318 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Should i use rigidbody for platformer movement? 1 Answer

Good Solution to Physics Based Gameplay 1 Answer

Player sticks to walls even with physics material 1 Answer

Rigidbody projectile misses collider 1 Answer

Adding velocity to an object in a brick breaker game 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