Today I was working on getting something to follow the player (I like zombie games ok) after some working on it and a few questions on Unity Answers, I have a working turning script to face an object or the player by default.
I however, when I do something I want to do more, make it pretty and add jetpacks and....ok I will stop there.
Basically, I want to know, how much further the NPC or such needs to turn to be facing the direction of its target.
So I always want to do it in the least amount of statements, but I believe I have found the minimum amount needed.
In the end I came up with the below, first find if the goal (rotation to face target) is above or below the value of 180, and set a Boolean accordingly.
Hence the circle above, this helped visualize the rotations for this.
So if the goal is on the right hand side of the circle, and the current rotation if on the left, then the value is the current rotation plus the distance from the goal to 360 aka the 0 on the other end.
There are 3 other calculations depending on the positions; Goal left or right, and Current position left and right; 4 in total.
I'm quite happy with the code, so there you go.
I however, when I do something I want to do more, make it pretty and add jetpacks and....ok I will stop there.
Basically, I want to know, how much further the NPC or such needs to turn to be facing the direction of its target.
So I always want to do it in the least amount of statements, but I believe I have found the minimum amount needed.
In the end I came up with the below, first find if the goal (rotation to face target) is above or below the value of 180, and set a Boolean accordingly.
Hence the circle above, this helped visualize the rotations for this.
So if the goal is on the right hand side of the circle, and the current rotation if on the left, then the value is the current rotation plus the distance from the goal to 360 aka the 0 on the other end.
There are 3 other calculations depending on the positions; Goal left or right, and Current position left and right; 4 in total.
I'm quite happy with the code, so there you go.