Hi
I’m trying to make a top-down camera that follows the mouse cursor but without loose sight of the character, so the camera is always between the character and the mouse cursor.
Something like Magicka Wizard Wars. Let’s say that the center of the screen is the position 0,0
When I move the cursor 20 “units” to the right, I want the camera to move 10 “units” to the right. That way, if the cursor is on the right-top edge of the screen, the player character will be on the left-bottom edge.
How do I do that?
I know that it’s possible to make the character look at the cursor, but I have no idea how to move the camera based on the cursor location.
Also, I don’t really care if I need to use c++ programming or blueprints, whatever works.
Thanks for nay help.
I haven’t messed around with the cursor at all yet, but on a high level I would do something like this: on each frame get the players x/y position and the mouse x/y position. Make sure the Z value is the same when you go to check the distance between those two points. Set the camera to where ever the mid point is [(x1 + x2)/2] [(y1 + y2)/2]. Take the distance that they are apart and move the camera up or down based on that. You will probably need to use some trig and take into account the FOV for that, not sure if blueprints can do sine and cosine.
For a smoother effect, apply a velocity and velocity dampening to the camera instead of just settings its position. I’m not sure if that would have any consequences though.
Thanks for your reply.
I actually already tried this, but with no success.
The node that give the mouse location is something like get Mouse Position Scaled by DPI, I don’t know exactly what it means, but it positions the camera in the wrong location…
About smoothing the effect, it’s not necessary since it’s updated every frame. At last seems smooth enough to me (just wrong positioned).
Did it try to move the camera closer to the origin of the world? If so I think what that does is just get the cursor’s screen space coords. So if the cursor is at the top left corner, that node will always return 0,0 no matter where you are in game.
You may have to do a ray trace from the cursor…if that is possible.
That’s actually not the best way of doing it (I mean the “Mouse Position Scaled by DPI”).
Could you try the same thing with “Get Hit Result Under Cursor by Objects”? This provides the hit result of a trace from your camera through your mouse onto the environment and therefore provides you with the location your mouse would click.
In my experience that’s the best node whenever you want to work with your cursor location in world space.
Cheers
I put a “print string” and I got just (0,0,0) no matter where my mouse is. I got the values that I want through the “Get Mouse Position” inside the Player Controller Blueprint, but I control my camera through my CharacterBP.
Do you know how can I get the communication between them?
I couldn’t get it working following others posts about that.
I guess the fastest way would be passing the mouse position vector from the controllerBP to the CharacterBP
hi chrystianz,
i tried you’re blueprint but it’s doing somethings strange. My character he’s always in the center of my screen but when i’m moving my camera, my character is moving also at the opposite of my cursor and i can’t move my character with the inputs can you help me pls ?
alt text
Hi, sorry that I couldn’t answer sooner, my computer died and I couldn’t buy another at the time.
Anyway, that blueprint is only for the camera, you should not bound any movement to it. What I did was use the character that already used the topdown click-to-move scheme and changed only the camera.
It shouldn’t be anything difficult, but I’ll install the UE4 in my new computer and see if anything changed in the latest version of the engine