# Using mouse click/drag to apply force to an object

**URL:** <https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870>\
**Category:** Blueprint\
**Tags:** force, unreal-engine, UE4-26, Blueprint, question\
**Created:** [August 23, 2021, 5:46pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870 "2021-08-23T17:46:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahlekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rahlekk](https://forums.unrealengine.com/u/Rahlekk)\
**Post date:** [August 23, 2021, 5:46pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/1 "2021-08-23T17:46:44Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 23, 2021, 7:51pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/2 "2021-08-23T19:51:18Z")

</div>

> 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

 ![347060-screenshot-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/a/d/9ad731060cba903e5ac16a1e13881dfa1f726531.png)

- intersection

 ![347061-screenshot-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/3/b/73b356041de1eee0c146a54e5242b84d674754a8.png)

[![Image from Gyazo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/d/d/4dd1d5cebbee1d009e231feab6e92ef0a86f1687.gif)](https://gyazo.com/372e5f3f321f57ed1eecd0cac1ab863f)

* * *

Project link so you can have a closer look:

> **[MyProject8.zip](https://drive.google.com/file/d/1pQa0F5UYSjwilMYeXiEA86OEmJlygFy0/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![Rahlekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rahlekk](https://forums.unrealengine.com/u/Rahlekk)\
**Post date:** [August 24, 2021, 1:01am UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/3 "2021-08-24T01:01:51Z")

</div>

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.

 ![347070-getintersectionpoint.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/a/f/7af82df405c1ad2c81fd63fd724f804edac846cd.png)

 ![347081-playercontroller-bp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/8/c/e8c8f7f4bc358fe6e5c30f4803801ee42f09211f.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 24, 2021, 6:17am UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/4 "2021-08-24T06:17:22Z")

</div>

> 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?

 ![347093-screenshot-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/3/e/53e5809755dda6b65f609fcf8f3f26753974a30d.png)

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?

---

<div class="post-metadata">

**Author:** ![Rahlekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rahlekk](https://forums.unrealengine.com/u/Rahlekk)\
**Post date:** [August 24, 2021, 3:38pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/5 "2021-08-24T15:38:37Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 24, 2021, 4:54pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/6 "2021-08-24T16:54:46Z")

</div>

> 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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/2/6/22642b9ed89fb21cedaae9eecdd5113c246942a8.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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 24, 2021, 5:06pm UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/7 "2021-08-24T17:06:39Z")

</div>

For example:

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

 ![347189-screenshot-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/1/f/61f0912e748201a7204297ce23a5adc990e767d7.png)

- this level is using it:

 ![347187-screenshot-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/6/7/d672649b73428fd555d31d5c7214af4d335d023f.png)

- so now we can jump:

[![Image from Gyazo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/f/7/af7d0164f8c7c37126cb406e15b0162ed57ae0fa.gif)](https://gyazo.com/fd98eece98d63be97dd7f9f41c85c5b7)

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:

> **[bouncingSpheres.zip](https://drive.google.com/file/d/1vYB5QMBMK7f7BND2eDSbgGYJ-lpu3us4/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![loldi1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@loldi1](https://forums.unrealengine.com/u/loldi1)\
**Post date:** [May 24, 2024, 3:31am UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/8 "2024-05-24T03:31:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [May 24, 2024, 6:09am UTC](https://forums.unrealengine.com/t/using-mouse-click-drag-to-apply-force-to-an-object/487870/9 "2024-05-24T06:09:13Z")

</div>

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:

> [@What are the methods avaiable to draw lines in unreal engine?](https://forums.unrealengine.com/t/what-are-the-methods-avaiable-to-draw-lines-in-unreal-engine/654052):
>
> Hallo everyone, I am exploring different methods to draw lines in unreal engine (my aim is to draw a rectangle projecting from a point using lines). I done some research and found some methods like using direct line method, debug draw, procedural mesh etc. It will be helpful if someone tells anyother method or methods which we can use to draw lines in unreal engine with c++ scripts or any other way?
