Making a RigidBody2D act like a homing missle

Godot Version

4.6.1

Question

Hey guys! I’m attempting what the title says and came up with this code":

func _process(_delta: float) -> void:
	linear_velocity = Vector2(randf_range(150, 250), 0).rotated(rotation)
	look_at(get_tree().get_first_node_in_group("player").global_position)

My RigidBody will face towards the player but won’t move. Without the look_at() line, it’ll manage to move. Not sure why. (Gravity scale is zero)

If you want full control over the velocity and rotation, consider using a CharacterBody (or even just an Area) for the missile. When using a RigidBody, you should use apply_central_force() instead of directly changing linear_velocity.

About the rotation: Its usually enough to just rotate the visual representation (the Sprite2D or whatever you are using). To constantly rotate the whole RigidBody you should use _integrate_forces():

This is from the Using RIgidBody page in the docs. The example on that page is for a RigidBody3D, but you could adjust it for a RigidBody2D.

1 Like

Don’t. Making it act like a missile would require implementing some sort of PID controller, which is probably not something you’d want to get into.

Just use some plain 3d node and script the homing movement.

1 Like

I realized that I shouldn’t have used a RigidBody. I was initially following the Your First 2D Game tutorial in the docs, and it uses that for some reason. Making it a CharacterBody2D solves it.