- Home /
Pushing and object up/down/left/right at constant speed
Hi, I'm making a 2D game and my character moves at constant speed in any direction using WASD. I want my character to be able to push crates, and when it does so I want it to move at a lower speed and the create must move at the same speed. My problem is the following:
Created a Boolean variable that indicates whenever the characters collider is in contact with the Crates collider and this in turn slows the players speed so that it pushes the crate at a lower speed. I reset the speed of the crate to 0 whenever the character and the crate get separated so that it stays in place.
However when pushing for "long" distances the crate somehow manages to get out of the characters reach (just by small distance) which make the player restore it's usual speed and causes the crate to stop, but then they eventually both get into contact again and the character moves again at a reduced speed along with the crate. This creates an undesired effect of the player moving not at a constant speed, and the crate suddenly stoping for an instant an then moving again, the bool variable turns OFF and ON even if the Player never stopped pressing the W to push it up for example.
Is there any way to make sure the crate never gets out of range so that the player keeps moving at a lower constant speed? I wish to keep the effect that whenever the Player stops pushing the crate (when the WASD keys are lifted) moves still a little bit farther so that that it stays out of reach and the player can move freely without accidentally pushing it again inmediately.
By the way both the character have a rigidbody2D with the gravity scale set to 0. And they have 2d Colliders as well. The characters rigidbody2d is set to never sleep due to toher functionalities.
Thanks for your help.