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.