How to get actor to follow the mouse?

So I am making an angry birds type game and I am trying to figure out how to make the projectile actor follow the mouse cursor while the player is pulling back to decide force and direction of aim?

I have it set up to where the player can pick which projectile they want to shoot and then once they do it spawns it in the “slingshot” area in which the player can then pull and release to shoot the projectile.

Thanks in advance :slight_smile:

Try the following logic (in the player controller). If you position an object at the resulting vector each frame, it should follow the mouse perfectly.

Note: Our camera is positioned along negative Y and we don’t have any rotation (it looks straight down Y). If yours is somewhere else, you may need to jiggle the logic around a little. This also presumes that your ‘game plane’ is at 0.0 Y in your world. Either way, it should get you started.

So this is what I have right now but it doesn’t seem to work.

When you say it doesn’t work, what exactly is happening? Some thoughts:

  • This is hooked up to the OnTick event?
  • If you print out the value of the vector (that’s going into ‘New Location’) what do you see? Is the value changing as you move your mouse?
  • What’s the position of your camera? Is it something like (0, 2000, 0)? Is it facing directly down the Y axis?

No but when I hook it up to the onTick event the camera goes crazy and looks like it gets pushed back. The value of the x changes the y stays the same. The camera is facing directly down the Y axis right now.

What happens now is that it will almost teleport to the left of the camera.

This shouldn’t have any affect at all on the camera. Is your camera anchored to that object that you’re pulling back?

Nope not at all.

Well, I’m not sure what to suggest at all now. All I can do is provide a few pointers:

  • You should have an object in the world or in a blueprint as a component.
  • The player controller should be generating the result of that vector (has to be the player controller, it has the mouse input stuff). This has to be done on tick (every frame).
  • You either need to give the player controller access to the object (so it can access it’s world location), or send the value of that vector to the object.
  • Use ‘Set Actor Location’ (Might be ‘Set Actor World Location’) and set it to the value of the vector.

If you’ve done it correctly, the actor should follow the mouse perfectly (or be doing something close, so you can work out what you need to change to make it work for you).

Regarding the camera. If the camera’s moving - maybe it’s being updated by the player controller… You’re not moving the player controller? (Or a pawn it’s attached to?)

How would I go about hooking this up to a click and drag with the mouse?

In the player controller, use the ‘Left Mouse Button’ node to start/stop dragging.