AI Help

So I’m trying to do a simple AI. What I’m trying to achieve is something like this:

Note that the pink dot is the AI, the black dot is the player. As you can see, the pink dot comes in and stops at a certain distance away from the player. Then it snaps to that relative location to the player. I’m going to guess I could do something where it does a Vector3.MoveTowards and then raycasts the distance from the player. Only problem is that I’m not that well-versed in raycasting. Any help would be appreciated!

You can make the AI automatically match the player’s movements easily:

AI.transform.SetParent(Player.transform);

(you may have to set the scale of your AI again, changing parents often messes with the scale of your child object)

Finding out the distance between the AI and the player is also easy once you know how:

float distance = (AI.transform.position - Player.transform.position).magnitude;

Physics.Raycasts are super cool and not too complicated, I highly recommend looking up a tutorial on them for future use, but I don’t think you’ll need them at all here.

1 Like

Set Parent would work, but you still have to get the AI in position so you have to do the work anyway.

How are you pathfinding? If you’re using NavMesh all you have to do is get the position of the player, add the offset of how far you want it (Transform.transformVector here if you want it relative ie always behind) and plug that into your agent.SetDestination() method.

Tada!

I guess the vector math here can be confusing if you aren’t familiar with it but that is the basic idea. Throw some code out here and we can help you make it work.