- Home /
Stop Character Drifting after using AddForce
Hi, i am creating my first own 2d Unity Project. i Made my character move with rb.Addforce, but after the character moves, and you let go of the button it takes a while before the character stands still again. it kinda drifts across and slowly loses acceleration. is there a way to minimize the time it takes for my character to stop moving, or tottally delete the drifting thing, or somehow set the acceleration to 0?. i tried this cuz i only want to affect the x acceleration but doesnt work: rb.velocity.x = 0f;
Im totally new to unity and sorry for my not too great English.
Answer by Timo326 · Sep 24, 2017 at 03:35 PM
Try to use a higher drag on the rigidbody. You can change it in the inspector or by script with rb.drag = 5 (for example). You may need a stronger force to move your character then.
To set the velocity.x to zero try this:
rb.velocity = new Vector2(0, rb.velocity.y);
Thank you! it worked for me perfectly. and also thanks for the way to change the velocity.x
Answer by TheMannyzaur · Sep 24, 2017 at 02:51 PM
If its 2d try using Input.GetAxisRaw to make him stop
Answer by benjiHilton · Sep 24, 2017 at 02:57 PM
right now my character moves with if (Input.GetKey(KeyCode.LeftArrow)) so i cant use input.GetAxisRaw. do i have to redo my script and change it to using axis instead of specifc keys. its just im making a 2 player game so then i cant use the same axis so move both of them.
using axis is way better than using specific keys. so yes i do suggest using axis rather than specific keys. but make a backup though for safety purposes
Your answer
