Hi, code in screenshot allows enemy approach player once player gets into enemy’s range.
Can anyone explain how playerObject.position - transform.position can calculate the direction of enemy towards the player?
Hi, code in screenshot allows enemy approach player once player gets into enemy’s range.
Can anyone explain how playerObject.position - transform.position can calculate the direction of enemy towards the player?
The way it works is that when you subtract the Vectors its like finding the difference of the two Vector,
lets break it down and use 1 axis for an example.
If the players position is 7 and the enemies is 2 when you subtract 2 from 7, you get 5, which is the direction the enemy needs to move, but 5 would be too fast and as we only want the direction the enemy needs to go in, we do .Normalize() to make it 1 to -1
Another example with 2 axis, if the players position is (-22,5) and the enemy is (1,9), -22 + 1 = 23 (Normalized = 1), 5 - 9 = -4 (Normalized = -1), so initially the enemy will move in the direction (1, -1) but because its in the Update function its being calculated every frame, so when the y level is the same as the player the direction will be (1, 0).
And it will stop when it is less than the playerChaseRange