How to Make My Player Rotate Towards Mouse Position

How do I make my player rotate towards mouse position?

See How to rotate an image(player) to the mouse direction?. What you want to do depends on which part of the player (top or right etc.) should be orientated to the mouse.

Don't compute and sum relative angles. Calculate the vector form the the player to the mouse:

player_x, player_y = # position of the player
mouse_x, mouse_y = pygame.mouse.get_pos()

dir_x, dir_y = mouse_x - player_x, mouse_y - player_y

The angle of the vector can be calculated by math.atan2. The angle has to be calculated relative to the base orientation of the player.

e.g.

The right side of the player is orientated to the mouse:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x)

The top of the player is orientated to the mouse:

angle = (180 / math.pi) * math.atan2(-dir_x, -dir_y)

A basic alignment can be set using a correction angle. For example 45 for a player looking at the top right:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45

The method update may look like this:

def update(self):
self.pos += self.vel * self.game.dt

mouse_x, mouse_y = pygame.mouse.get_pos()
player_x, player_y = self.pos

dir_x, dir_y = mouse_x - player_x, mouse_y - player_y

#self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x)
#self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45
self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)

self.image = pygame.transform.rotate(self.game.player_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos

Minimal example: Sample Image repl.it/@Rabbid76/PyGame-RotateWithMouse

Sample Image

Function to rotate player with mouse position is based on mouse distance instead of position

First things, you seem to imply that there you have this code on two different objects. You should create a single script called "LookAtMouse". Whatever you put this on if what will look at the mouse.

public Camera cam;
void Update() {

Vector3 direction = Input.mousePosition - cam.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

}

So this should be only on the player.

How to rotate player towards mouse position with a moving camera using cinemachine and input system? Unity 2D

You can try using Mathf.SmoothDamp() to make sure the angle doesn't change too much at once.

https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html

something like this (assuming this code is running in Update()/FixedUpdate()/LateUpdate()) :

// class member variables
[SerializeField] private float _smoothTime = 0.3f;
private float _velocity = 0.0f;
...
...
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
if(angle != 0)
{
savedAngle = Mathf.SmoothDamp(savedAngle, angle, ref _velocity, _smoothTime);
}
transform.rotation = Quaternion.Euler(0, 0, savedAngle);

Rotate the user character on mouse position

Here's an image illustrating the situation:
Image

You have the mouseY, playerY and mouseX, playerX

Therefore you can calculate the height and base of the triangle,

Thus the angle with tan
However, since in the second and third quadrants y/x will return an angle in the first and fourth quadrants, you need to use the Math.atan2(y,x) function in Javascript, not Math.atan(y/x). This will give you an angle between -180 and 180 instead of between -90 and 90.

Atan Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2

Then all you have to do is rotate based on the angle!

(P.S. Remember that you will have to convert between radians and degrees)



Related Topics



Leave a reply



Submit