RigidBody not moving when standing still for a few seconds

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Astro Games

Hello,

My RigidBody2D is not moving when I have not pressed a movement key for a second or 2. What could cause this problem? The RigidBody2D is in Character mode because I don’t want it to rotate. The Player.gd script has the following code:

extends RigidBody2D

const MAX_SPEED = 200

var motion = Vector2()

func _physics_process(delta):	
	if Input.is_action_pressed("ui_right"):
		motion.x = MAX_SPEED
	elif Input.is_action_pressed("ui_left"):
		motion.x = -MAX_SPEED
	else:
		motion.x = 0
	
	pass

Thanks!

:bust_in_silhouette: Reply From: lincolnpepper

For non-rotating rigid bodies you should generally use KinematicBody2D, which is essentially the same thing. Try switching the type and using move_and_slide(motion). Is that the whole script? If not, can you paste the whole script?

Hi,
I chose to use RigidBodies as I want the player to be able to push certain objects, and I couldn’t find a way to push other objects around when the player is a KinematicBody2D.
The script I posted was the full scripts. It’s very basic, I know!

Astro Games | 2018-04-10 11:40

There’s a way to make the player push stuff with physics, but chances are you want to have control over your pushing and make it work more like pushing blocks in ocarina of time. There are ways to do that that wouldn’t need any of the extra properties of rigid body. But either way, the script doesn’t even allow the player to move since you don’t have anything like linear_velocity += motion. Motion is not a property of rigid body, so changing motion does not change player movement. And because of the way rigid body works, you have to change the built in linear_velocity to move it.

lincolnpepper | 2018-04-10 14:23

It probably didn’t copy right… I have set_applied_force(motion) at the end of the script. But you’re saying that I should use a KinematicBody?

Astro Games | 2018-04-10 15:18

You probably should use KinematicBody2D, and i would with the parameters you have given for the character. But if you want to use a rigid body, that’s fine too. I tried using set_applied_force(motion) and i had no problems. Maybe it’s something with your parameters? If you upload the project on github or dropbox and send me the link i can try to figure it out from there.

lincolnpepper | 2018-04-10 16:27

:bust_in_silhouette: Reply From: markopolo

If you can move the body, and then leave it alone for a few seconds, and then can’t move it, it might be going to sleep. You can disable the ‘Can Sleep’ option, or just apply a force or movement and it should wake up again. More information is in the physics docs.