Fully disabling movement for RigidBody2ds

Godot Version

latest

Question

Im Trying To Make A 2D Top Down Fighter Game And I Was Looking For An Object That Could Stop CharacterBody2ds That I Was Gonna Use For Obstcales And Decided To Use RigidBody2ds But Even Do Gravity is set to 0 It Still Moves When Comes In Contact With a CharacterBody2d. how can i prevent this?

player code:

func _process(delta):
	look_at(get_global_mouse_position())
	if(!Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)): # Debug
		position.y += sin(rotation)
		position.x += cos(rotation)

It depends on whether you want your obstacle to be moving.

If you want your obstacle to stay still (i.e. be static), you should use a StaticBody2D instead of a Rigidbody2D.

If you want your obstacle to move while still blocking the player, you should use a RigidBody2D node with its freeze-setting set to true and its freeze_mode-setting set to FREEZE_MODE_KINEMATIC.

You can read more about the two node types in Godot Docs:

i have tried static bodies and they did not stop the player

Did you add a CollisionShape2D to this StaticBody2D?

yes i have done that

Altering the position directly will not interact with physics. I recommend your player be a CharacterBody2D and other stopping-objects be A StaticBody for walls and RigidBody for anything the player could push.

Check out the template for CharacterBody2D’s movement, the key is it sets velocity then uses move_and_slide() all inside _physics_process(delta: float):

Try these changes out!

extends CharacterBody2D

# ...

func _physics_process(delta):
	look_at(get_global_mouse_position())
	if(!Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)): # Debug
		velocity.y = sin(rotation)
		velocity.x = cos(rotation)
	else:
		velocity = Vector2.ZERO
	move_and_slide()
1 Like

thanks for your help

dude that didnt work. the velocity change is not affecting the player.

nvm fixed it thx

Do you mind expanding on what fixed it? Honestly the change to velocity should’ve been the first step to a proper solution, I did not understand the original post all that well :sweat_smile: