I want to get the angle between the mouse cursor coordinates and the player.

Hello !
I want to find the angle between the two vectors

I’ll show you the picture


It looks like this in the actual game, and I want to get that angle.

This project is Top-Down view

I’ve tried several ways, but I don’t get the result I want, so I’m leaving a question like this.

I would appreciate your help !

1 Like

:smiley:

3 Likes

edit: or just use ClockworkOceans better answer.

Lets call the gun direction a and the direction from the gun to the mouse b. On top of that lets call the gun location A and the world space location of the mouse B. I assume you already have a and A. You can get B with “get hit result under mouse” and b can be calculated with B-A, you also need to normalize this value.

Now with a and b you can calculate the dot product, that is a value between -1 and 1. Add 1 to the value of the dot product and multiply by 0.5. Now subtract this value from 1. This value is 0 if a and b are the same direction and 1 if they are the opposite direction. Multiply the value by 180.

If you need to know does the angle rotate up or down you need to do this next step. You have another direction c, that is perpendicular to the gun direction (gun forward vector). c is the up vector of the gun. You get the dot product for b and c, if this value is less than 0 you multiply the value of the previous step by -1, if it’s 0 or greater then you multiply by 1.

3 Likes

Thanks for the information!
But I already looked it up before I posted the question,
I didn’t get the result I wanted when I did it like that…

I tried as you told me, but the expected result didn’t come out.
I’ll try another way !
Thank you for helping me !

dot product→acosd = angle.
but top down view?are you going to make an aim offset?

Exactly !
I need an angle between the position that the mouse cursor points to
and the character position as a variable of aim offset.

The function that already finds the angle has been implemented with help.

However, I think there is a problem finding the two vector parameters.

Is there any additional consideration for Top Down View?

Those two parameters are directions:

But top down aim offset doesn’t need so complicated method.
you could probably have a look at the previous post.How do I get the character to aim up or down TDS? - #9 by baobao4435

It’s a little ignorant, but I solved it anyway.

When I made BP in this way, I got the desired result value to some extent.

Here, we implemented it to be processed naturally by applying Sample Smoothing to the conditional statement and Aim Offset according to the situation.

edit: you figured it out on your own

Assuming your function works as expected you can use the following vectors as the inputs: one that points to the mouse position from the player and one that points to the position that is at the same location as the mouse position, but with the z being swapped for the character position z.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.