How would I make enemies constantly find the angle between a player and whatever object the player is protecting,

Heya there.

I’m working on a Muffin Kombat 2 inspired game (And for anyone wondering what Muffin Kombat 2 is, it’s essentially just a tower-defense game with enemies that’ll either attack the player is near or attack whatever the players are protecting which I’ll be referring to as “Objective”).

Anyway, I’m trying to recreate the targeting system used from Muffin Kombat 2 using raycasting and PathfindingService. I decided to ask help on Discord on how I can implement the two. Someone told me that I should make enemies constantly find the angle between a player and the objective. If the degree is greater than 90, attack the player. Otherwise, attack the objective.

The issue being is that I don’t know how I can do such since I’m still a fairly new scripter with little to no raycasting experience. So how can I do something like that?

Imagine there is an finite plane. On that plane, we have a circle named Z.

image

Currently, Z has no friends. He cannot see his surrounding environment. He is blind.
Now, we shall pretend that Z was given the gift of sight. This gift is known as “raycasting”. Unfortunately for Z, he has absolutely zero clue on how to use his new gift. We must give him a set of instructions.

Z, look 100 pixels in the positive X direction

image

Uh oh! It looks like Z couldn’t make it 100 pixels. Something is in the way! Maybe we can try again, this time with a more specific set of instructions.

Z, look 100 pixels in the positive X direction and return any relevant information.

image

Woah! Circle Z found Circle A! This is useful information that explains objects within the plane, relative to Z.

Now that you have a basic understanding of Raycasting, try and apply these fundamentals in your creation. Here are some resources to get you started. YouTube is also a great place to find step-by-step guides if you have trouble following along.

I hope this helps!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.