Why doesn't that work correctly.

Physics.Raycast(transform.position, enemies*.transform.position, hit, 50, 9);*
This here should raycast straight to the enemy, right ? Well, if an enemy get’s in range, so the raycast will get checked, the raycast appears to point statically in some random direction without moving. I checked the raycast with Debug.Drawray(). I found some threads about raycasting to moving objects, but no one had given an answer, that has helped me.

because thats no direction.

you can’t to have enemies*.transform.position - transform.position in the second field to cast in the direction from you towards enemy i*

You can use Physics.Linecast to check between two points. But raycasting needs a ray, as the name implies, either as an actual Ray or as the separate origin/direction components.

–Eric

Well how do I raycast from object A to object B than ? Or I must only use linecasting ?

Nevermind, I’ll do it with linecasting…

You can use Raycast if you like but you must generate a proper ray first or at least get a direction.

look at the Ray class constructors.

I was trying to do that, but didn’t succeed. Nevermind, I got it working with linecasting.

“target.transform.position – transform.position” will give you a vector from the one point to another. “.normalized” will give you a normalised direction vector.

So something like:

var direction: Vector3 = (target.transform.position - transform.position).normalized;

What I’ve found to be more useful if you want to check if an enemy can see the player and reach them would be to use something like CapsuleCast or even multiple raycasts, perhaps one from the enemy’s knees to see if they can walk to you, and another from their head to check if they can see you. You also have SphereCast which is comparable to a “thick raycast”. You may also have to consider the possibility of there being a hole in the ground between the enemy and the player or other such obstacles, depending on the type of game you’re making.