- Home /
How can i change the width of the raycast?
My enemy see with raycast but the is just a very small location.I want to make bigger raycast because then that can see me easier.(Sorry for my english)
Take a look at Physics.SphereCast(). There is also a Physics.CapsuleCast() depending on what you are trying to do.
Answer by Owen-Reynolds · May 17, 2013 at 04:12 PM
A wider raycast will see you less easily. Suppose montsers are peeking over a wall and past a tree. A wider cast would hit one of those, and think it can't see you.
The trick is, a raycast can see you if it hits you OR if it hits nothing. It it hits nothing, that meant it went through your armpit, maybe, but there was nothing else in the way. It might hit something behind you, but you can compute how far away the player is and only ray that far (which runs faster.) Something like:
bool canSeePlayer=true;
// this will really be from raycast start to player chest or head:
float dToP = (player.pos - monster.pos).magnitude;
// the only way to not see player is we hit something in the way:
if(phy.rayCast(...hit, dToP))
if(hit.transform!=player) canSeePlayer=false;
Raycasts aren't super easy, but there are lots of people using them here, with examples.
If @Ownen Reynolds answered your question, click the checkmark next to his answer to close the question out.