Hello, I have a player object which I want to change it’s animation to Up if it’s target is above it Right if it’s target is beside it, and down if it’s target is below it. My player moves towards an object which follows the mouse. Here’s the code I tried where 1 is up 0 is right/let and -1 is down.
It uses the keys but it should give you enough of an idea to do it with the mouse position too, let me see if I can find something more specific though.
Yes, the animator isn’t really the problem, It’s my code. If my target is on the left of my gameobject the values are negative, and If it’s on the right the values are positive, I’ve tried it normalized and not normalized. So sometimes the Y is the opposite of the X (positive and negative wise) and other times they are the same (positive and negative wise) which makes my code (this line) if ((mouseAim.position - transform.position).normalized.x > (mouseAim.position - transform.position).normalized.y) useless. Is there another way of calculating this? Thanks
I think you’ll have to wait for someone more experienced to come along I’m afraid, I’m sure it can be done though, it’s just a matter of knowing the right maths.
To find out if it’s to the right or the left, just check if mouseAim.position.x > transform.position.x, and to check if it’s above or below use mouseAim.position.y > transform.position.y.
Yes that’s why I was calculating the distance between the Y and the X to know which one was greater but it would go into the negatives. So if the mouseAim.x is greater than transform.position.x and mouseAim.y is greater than transform.position.y, how do I make my object know which sprite to change to? (sprite Right or sprite Up), please help