How to Drag Object in 3d space?

Hi all, I’ve been trying to figure out how to click and move (drag) an actor in 3d space using C++.

My plan was to:

  1. Select object with click and update a pointer to point to the grabbed object. (this is working)
  2. In Tick method, move object to mouse location projected onto the floor.
  3. On click - release at mouse location.

However I have not been able to figure out how to translate mouse x,y to world space in the tick method in way that will work. (I suppose I could fire back down looking for impact position on selected actor and move it, but I feel this could fail if mouse moves to much in tick internal).

Looking for any pointers?

This is what I’m doing in the click method:

controller->DeprojectMousePositionToWorld(worldLoc, worldDirection);
		FHitResult hitResult;
		bool bHit = controller->GetHitResultUnderCursor(ECollisionChannel::ECC_GameTraceChannel1, false, hitResult);
		if (bHit)
		{
			grabbedActor = hitResult.GetActor();
		}

After a bit more playing around, I have come with this ‘hack’.
I have 2 trace channels (1 for objects that can be picked up, and 1 for the floor).

So in the Tick method, I search for a HitResult on the floor (using floor trace channel), and move the grabbed object to that location. This feels wrong, but its working for now.

Hi @Danomano

Here is the Blueprint solution of your problem.
Let me know if you want help to make this as a C++ function

deproject is from gameplaystatics:

1 Like