Changes sprites to look at mouse location
I want to have an player where the sprite changes to look at the mouse, something like this
right now im just using a bunch of trigger polygon colliders to detect when the mouse enters them, but this seems really inefficient and dosent account for the camera zooming out
Answer by Rynbernsz · Sep 06, 2018 at 01:59 PM
Could you just compare the transform.position of your main character to the transform.position of your mouse? If you have 8 directions, you could do something like this but mess with the ranges for the X, Y comparing.
lookDirectionX = mouse.transform.position.x - player.transform.position.x;
lookDirectionY = mouse.transform.position.y - player.transform.position.y;
// Mouse is to left of player
if (lookDirectionX <= 0) {
if (lookDirectionY < 0) {
// Face character to LEFT, DOWN
} else if (lookDirectionY > 0) {
// Face character to RIGHT, DOWN
} else if (lookDirectionY == 0) {
// Face character LEFT
}
}
That's how I would first try it, anyway. Let me know if that works!
Your answer
![](https://koobas.hobune.stream/wayback/20220612174058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with a rendering of a 2D sprite 1 Answer
Depending on the mouse movement (or position), the player sprite 0 Answers
Animation Panel: how to turn an existing loop into just a standing sprite and then implement it 0 Answers
Animator prevents OnMouseDown() 0 Answers
Sprites moving (shaking) during animation while they should not 1 Answer