Using mouse click/drag to apply force to an object

Hey all. Been trying to figure this out for a few hours with no luck.

The concept of my idea is essentially similar to pool/billiards. You have a top down view, and you’d click on the ball, drag in a direction, and release, and the ball would shoot in that direction, with the force applied being larger the longer your drag was from the object.

Basically the logic in my head is something like:

  1. When you click, set a vector for the position of the ball actor
  2. When you release, set a vector for the position of the mouse
  3. Then, apply a force to the ball as some combination of the above vectors 4. Then some other stuff that probably doesn’t matter for now such as, you can only do the starting click on the ball actor, as opposed to anywhere in the level

However I’m having a few issues.

When I put the logic in the Sphere Blueprint (the ball), I can only get the mouse click/release to work when the cursor stays on the ball. (I want the initial click to be the ball, but the release can be anywhere)

So I tried to put the logic into the Player Controller, but I can’t seem to figure out how to reference the sphere in the Player Controller. I get the error where I don’t have an instance specified. In normal blueprint to blueprint communication, I can specify the instance in the details menu, but for the Player Controller blueprint it seems to be different.

Technically I have no player controller. Since it’s a top down puzzle game, the player is “god”, and I have my camera actor set to “Auto Activate for Player: Player 0”.

To be honest, I’m not sure if I’m approaching this correctly. Perhaps I need to make the ball a pawn, so it gets possessed by the player? But then I don’t know how the camera would work, so I don’t think this is right.

Currently in my level I have these elements (excluding things like light actors): Camera Actor (gets auto activated), Floor, Sphere_BP (the ball that should get moved).

Essentially I nothing more than the standard Puzzle template, outside of a new map with a floor and a ball on it.

If anybody could point me in the right direction I’d really appreciate it! Just struggling on where to put this logic.

As some additional notes, I did look at this thread: https://answers.unrealengine.com/questions/328918/drag-back-object-and-release-to-apply-force.html

And this looks like what I’m looking for, however I’m having trouble figuring out WHERE to put the logic, as I mentioned above.

Thank you!

1 Like

So I tried to put the logic into the
Player Controller[…]

That’s where it belongs, you need a blueprint for handling input - this is perfect. Pawn is also good; and you can have the camera controls in the Pawn, and it meshes well with the player controller since it owns the camera manager. It’s a love triangle of sorts!

The concept of my idea is essentially
similar to pool/billiards.

Consider the following:

  • in the player controller

  • intersection

Image from Gyazo


Project link so you can have a closer look:

2 Likes

Thank you so much! I’ve been trying to replicate your code instead of just copying it outright.

I’ve run into a problem that I’ve been trying to fix, and it’s that the Impulse is only added to the -Y value, regardless of where the arrow is drawn.

I’ve attached the recreated code below.

1 Like

I’ve run into a problem that I’ve been
trying to fix, and it’s that the
Impulse is only added to the -Y value,
regardless of where the arrow is
drawn.

Hm. The script looks fine (at a glance). Things to double check:

  • ensure the simulating Sphere BP has the static mesh as the root
  • perhaps one of the constraints was accidently selected?

Could you confirm the two?


Also, could you show the hierarchy of the SphereBP - could there be anything in the actor that is interfering with the sim?

1 Like

I didn’t have the Static Mesh as root, once I fixed that I got the behavior that I expected!

However, after thinking about this for a while I did think of an issue. If I ever wanted to change the controls of the ball on a level per level basis (for example, adding jumping for certain levels only) I’d run into an issue since this code is in the Player Controller.

I understand this wasn’t in the initial scope of the question.

I’m still thinking on this…

1 Like

If I
ever wanted to change the controls of
the ball on a level per level basis
(for example, adding jumping for
certain levels only) I’d run into an
issue since this code is in the Player
Controller.

  • that is precisely what the controller is for, again, ha!
  • first of all, you can have more than 1 controller
  • and you can extend controllers via inheritance (we already do, anyway)

347179-screenshot-1.png

  • use the Main Controller for level 1 (only poking the spheres)
  • use the child of the Main Controller for level 2 (the child knows how to jump the spheres, but it also knows how to poke since we’re inheriting that functionality from Main)
  • the whole idea is not to repeat any script
  • if you want some functionality that is unique a level, you could script it in the level blueprint but I’d avoid that for controls unless it’s something truly unique
  • alternatively, you can implement the jumping in the entity that jumps, also fine

Who is doing the Jumping? The spheres? It might be as easy as implementing the jumping in the same controller and having a flag whether something can or cannot jump in a level.

For example:

  • the child PC has only this (the rest is in the parent)

  • this level is using it:

  • so now we can jump:

Image from Gyazo

Consider it. Level 2 is using another Game Mode, with different rules and different control scheme handled by an inherited PC child that can make the spheres jump.


Updated Project with 2 levels and additional jumping behaviour in the 2nd one:

1 Like

Thank you for this, exactly what I was looking for. I even adapted it slightly so that I can click+drag from anywhere as opposed to directly on the object which is something I needed.

Is there a direction you can point for me to look into drawing a similar ‘arrow’ not as a debug item?

You could use:

  • a spline + spline mesh component (3d)
  • a widget component + image (3d)
  • a widget + image (2d)
  • niagara ribbon particles 3d (but theres a plugin that allows the UI to render Niagara)
  • material 2d / 3d

Scrub through this thread for more ideas / examples: