Most tutorials only show you how to move towards the player, and that’s great and all. But i would love to know how to move my AI back to a certain position once its reached its destination and start attacking again. I’ve been working on this for weeks i can’t figure it out
if (playerDistance < 500f )
{
if (playerDistance > 5f)
{
// prevents enemy from getting to close to player
Chase();
transform.LookAt(player);
Attack();
}
}
Well, I think if you’re using > 5 and < 5 it will be “stuck” sort of, right, because it’s always going “close, away, close, away, etc…”? Is that what you’re saying?
Well, don’t give up on yourself too soon. Just takes some practice to think about things and how they work (or might work lol).
Okay, well, I would imagine that there is a certain range in which the enemy can attack ,right?
So, maybe once it gets that close, it starts attacking… then if the player is too far, it goes closer.
If the player gets closer, though, the enemy doesn’t have to move right away, but rather it moves if the player is “too close”.
So, it might look something like:
Range is 3- 8 (just for an example)
approach player while farther than 8 (or whatever number).
At any time, should the player be closer than 3, move away. Farther than 8, approach.
You could tweak that a little to try to get the enemy to “stop” in the middle (maybe 5 or 5.5, that way they’re not always on the edge… but they can start attacking before they’re done moving - unless that’s not allowed).
haha… All depends on how you go about it. For starters though, I’d say just do the range thing:
When you say the moving farther away is tough, how so? Do you mean like the enemy might walk into a wall or off of a cliff? lol I’m not very familiar with path/pathfinding stuff, and not even sure if that’s what you meant.
As for generally just “moving backwards”, I think the code I wrote above should work.
transform.forward is a forward based on object’s position, whereas (in some other code you wrote) Vector3.forward and the like, are world ‘forward’/‘/backward’/‘right’ etc…
If there’s some other complication not mentioned, come back n explain it after you code the little bit about staying in the range and maybe I / someone else can help you sort that out
Sorry i have to get better at clarifying. In the example Range is 3- 8 when the enemy AI is less than 3 move beyond 8. But if the enemy Ai starts move forward at 8 how to move beyond the 8 range
Not sure what you mean by move beyond the 8 range.
Let me rephrase just in case something could have been better written:
From the enemy’s perspective:
Slightly expanding on my previous answer…
if chasing and player > 5, approach.
if chasing and player <= 8 attack (+ continue approach)
if chasing and player < 5 , no longer be in “chase” mode. Just attacking
if player < 3 backaway until 5 or 8
if player > 8 in attack mode, attack is stopped and chase (resumes).
This could be simpler to have a trial run, though.
If player > 8 chase.
else if player <= 8 and >= 3 attack
else if player < 3 MoveBack.