How would I make the character always at the mouse position?
Hello Pikachu1001000,
that’s actually a quite complicated question. The easiest way would be to teleport the player at the exact position of the mouse cursor every frame. The “teleporting” part is very important because it would allow us not to worry about differ mouse speed which people have.
You can use this function APlayerController::DeprojectMousePositionToWorld:
Then set the character world position to deprojected world position with this:
Repeat every frame. Remember, that you can show or hide the mouse cursor using PlayerController api (good for testing):
Also, there might be some problems with collision so be sure to uncheck sweep and I’m not sure how the function to deproject mouse position would behave without any floor.
The second part which I’d like to consider is to actually order the character to move to the cursor world location. You can do this without worrying about speed of the character but it would totally change the resulting mechanics. The slower character would try to chase the quickly flying mouse, changing the direction of movement before reaching the previous placement of the cursor.
In this case you would have to get the value of mouse delta:
Then calculate the speed of the mouse by dividing delta location by tick delta seconds. Probably, it should be done using vectors in real world, so deproject two screen mouse locations to world space, then calculate the displacement:
https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FVector/op_sub/1/index.html
and the result put here:
https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FVector/Size/index.html
Now you have the delta of the mouse in world space displacement float, aka path. Now easily divide path by time and you can affect character using this computed speed. In this case just use:
Hope it will help.
Regards,
Taberu
P.S.
void ALongpakt::SetLocation() { APlayerController* MyController = GetWorld()->GetFirstPlayerController(); (...)
This is a very dangerous style. I’m aware that UWorld is usually there when we consider an actor existence but we should always assume it might be NULL, then the operator → would crash. GetFirstPlayerController is even more possible to be NULL. We have to check both inside of the body function anyway, so I think this style is exaggeration. I don’t even know how it compiled when you didn’t assign default values to second argument of the function (default values should be last).
But please, don’t consider it as an attack, I just have to express my opinion, I might be wrong. Cheers!