r/godot•
gw935

How can I make a RigidBody3D follow the mouse, while still interacting with other RigidBody3Ds?

help me

Hi, I want to have a RigidBody3D that follows the position my mouse is pointing at. I also want to use it to knock other RigidBody3Ds around, so just setting it to the position my mouse is pointing at doesn't work.
I managed the mouse position part, but I have trouble with the RigidBody3D mouse following part. I thought I could just do it like the following code, but the object just rubberbands around my mouse.

btw apply_central_force() does the same, just far slower.

const RAY_LENGTH : int = 1000.0
const HIGHT_OFFSET : Vector3 = Vector3.UP

@onready  var camera : Camera3D = $Camera3D
@onready  var mouse_object := $MouseObjectRigidBody3D
@onready  var thing := $RigidBody3D

var ray : RayCast3D

func _physics_process(delta: float) -> void:
  var from : Vector3 =    camera.project_ray_origin(camera.get_viewport().get_mouse_position())
  var to : Vector3 = from + camera.project_ray_normal(camera.get_viewport().get_mouse_position()) * RAY_LENGTH

  var query = PhysicsRayQueryParameters3D.create(from, to)
  query.exclude = [mouse_object, thing]
  var space_state = get_world_3d().direct_space_state
  var result = space_state.intersect_ray(query)
  if result.is_empty():
    return

  var target_position : Vector3 = result.get("position")

  print("target_position " + str(target_position))
  print("mouse_object " + str(mouse_object.global_position))
  print(target_position - mouse_object.global_position)
  #mouse_object.apply_central_force(target_position -  mouse_object.global_position)
  mouse_object.apply_central_impulse(target_position - mouse_object.global_position)

Posted Jul 11, 2026, 8:58 PM UTC

12
Loading comments…