Hello!
I’m currently trying to make a simple AI for a spaceflight game, and I want to “cap” how close the AI can be to the player, I made a script using the transform.position.magnitudes to make sure they didn’t get closer then the desired offset. However it aint working and I have absolutely no idea of why.
if(-offsetfromplayer <= (enemytransform.magnitude - this.gameObject.transform.position.magnitude) && (enemytransform.magnitude - this.gameObject.transform.position.magnitude) < 0f){
movementspeed = 0f;
localvelocity = new Vector3(0f,0f, movementspeed);
}
offsetfromplayer is 50 by the way. Which means that -offsetfromplayer is -50.
As you can see this is when the magnitude difference is negative, and it is negative when I do the tests. I know that because I’ve got the magnitude differences show up in the inspector. So that is updating correctly, -offsetfromplayer also show up correctly in the console.
And that should work as long as the magnitude differences is between -50 and 0. Right?