Find 1st target with spherecast

Hey all, working on a simple game but I can’t quite figure out a small selection issue. My game is a turn based strategy and I need to find enemies that I am shooting towards. I’m opting for a spherecast to detect an enemy since it will give a bit more wiggle room when trying to be accurate.

So I have my player, a location somewhere on the map they have clicked to aim towards, and an enemy directly between those 2 points. How do I find that enemy with a spherecast?

I’ve gotten a little close with following the manual for setting up the spherecast, but the direction is always world forward and not in the direction of the clicked area. So the first question is how do I set the vector direction to be towards my clicked location and not forward?

Any help at all would be great!

With vectors you can subtract one from the other to get the difference.

Lets say your player is at player and where you clicked in the world is at clicked (each one a Vector3).

Then you can write:

var delta = clicked - player;

Delta will now be a vector from your player do your click point.

You can just use this directly in your Spherecast call from the player froward.

Oh that’s so simple! And works perfectly! Thanks!

1 Like