playMaker

Author Topic: Best way to use raycasts for line-of-sight?  (Read 8671 times)

Adam Z

  • Full Member
  • ***
  • Posts: 207
Best way to use raycasts for line-of-sight?
« on: March 28, 2014, 10:34:00 AM »
Is it possible to send out multiple raycasts when setting up line-of-sight for an AI? (See example) I'm trying to develop a "cone" of vision, so that way targets can either be crouching and/or atop of buildings, etc.

If someone has another solution, I would love to hear it!

Thanks.

Adam Z

  • Full Member
  • ***
  • Posts: 207
Re: Best way to use raycasts for line-of-sight?
« Reply #1 on: March 28, 2014, 10:58:26 AM »
I could use game objects and set them up in the correct positions, sending out rays from those, but maybe there's an easier way?

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Best way to use raycasts for line-of-sight?
« Reply #2 on: March 28, 2014, 12:10:13 PM »
taken from the unity answers:

You want to calculate the direction between the enemy and the player, get the forward vector of the enemy, and then use Vector3.Angle to calculate the angle.

Then you can check if the angle is less than whatever angle you want to be the enemy's field of vision (eg: a 20 degree cone, for instance), and if it is, then the player is in that enemy's field of vision.
----
so in playmaker terms, use the vector3 operator, set up two vector 3 variables and set it to Angle. Get the players vector3 and if it is in that range you use "You can Store Magnitude in Get Axis Vector and use Float Compare on that..."

and that is another way to do it!

---

I also found this vector3 compare that Julius created. http://hutonggames.com/playmakerforum/index.php?topic=3058.0

« Last Edit: March 28, 2014, 12:13:36 PM by sebaslive »
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

Adam Z

  • Full Member
  • ***
  • Posts: 207
Re: Best way to use raycasts for line-of-sight?
« Reply #3 on: March 28, 2014, 12:16:33 PM »
taken from the unity answers:

You want to calculate the direction between the enemy and the player, get the forward vector of the enemy, and then use Vector3.Angle to calculate the angle.

Then you can check if the angle is less than whatever angle you want to be the enemy's field of vision (eg: a 20 degree cone, for instance), and if it is, then the player is in that enemy's field of vision.
----
so in playmaker terms, use the vector3 operator, set up two vector 3 variables and set it to Angle. Get the players vector3 and if it is in that range you use "You can Store Magnitude in Get Axis Vector and use Float Compare on that..."

and that is another way to do it!

---

I also found this vector3 compare that Julius created. http://hutonggames.com/playmakerforum/index.php?topic=3058.0

Thanks! How does this take into account obstacles blocking your view? With Raycasting the ray stops on the closest object.

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Best way to use raycasts for line-of-sight?
« Reply #4 on: March 28, 2014, 12:24:24 PM »
hmm, that is a good point. have you messed around with the pathfinder package? http://hutonggames.com/playmakerforum/index.php?topic=2540.0

or indieRAIN?
---
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

Adam Z

  • Full Member
  • ***
  • Posts: 207
Re: Best way to use raycasts for line-of-sight?
« Reply #5 on: March 28, 2014, 12:29:48 PM »
Thanks! That will definitely help with pathfinding.

Uttpd

  • Junior Playmaker
  • **
  • Posts: 70
Re: Best way to use raycasts for line-of-sight?
« Reply #6 on: March 28, 2014, 12:31:25 PM »
taken from the unity answers:

You want to calculate the direction between the enemy and the player, get the forward vector of the enemy, and then use Vector3.Angle to calculate the angle.

Then you can check if the angle is less than whatever angle you want to be the enemy's field of vision (eg: a 20 degree cone, for instance), and if it is, then the player is in that enemy's field of vision.
----
so in playmaker terms, use the vector3 operator, set up two vector 3 variables and set it to Angle. Get the players vector3 and if it is in that range you use "You can Store Magnitude in Get Axis Vector and use Float Compare on that..."

and that is another way to do it!

---

I also found this vector3 compare that Julius created. http://hutonggames.com/playmakerforum/index.php?topic=3058.0

Thanks! How does this take into account obstacles blocking your view? With Raycasting the ray stops on the closest object.

The method described "just" checks if the target is in the cone of vision. If positive. Then you can perform a ray-casting -> fire one ray to your target to check if its blocked, If it hits the target then "tango is in sight".
This way you use only one ray per frame (or every 5 frames etc)


Adam Z

  • Full Member
  • ***
  • Posts: 207
Re: Best way to use raycasts for line-of-sight?
« Reply #7 on: March 28, 2014, 12:40:23 PM »
That makes sense. Let me see if I can get that to work. Thanks!

Adam Z

  • Full Member
  • ***
  • Posts: 207
Re: Best way to use raycasts for line-of-sight?
« Reply #8 on: March 28, 2014, 01:11:33 PM »
taken from the unity answers:

You want to calculate the direction between the enemy and the player, get the forward vector of the enemy, and then use Vector3.Angle to calculate the angle.

Then you can check if the angle is less than whatever angle you want to be the enemy's field of vision (eg: a 20 degree cone, for instance), and if it is, then the player is in that enemy's field of vision.
----
so in playmaker terms, use the vector3 operator, set up two vector 3 variables and set it to Angle. Get the players vector3 and if it is in that range you use "You can Store Magnitude in Get Axis Vector and use Float Compare on that..."

and that is another way to do it!

---

I also found this vector3 compare that Julius created. http://hutonggames.com/playmakerforum/index.php?topic=3058.0

Thanks! How does this take into account obstacles blocking your view? With Raycasting the ray stops on the closest object.

The method described "just" checks if the target is in the cone of vision. If positive. Then you can perform a ray-casting -> fire one ray to your target to check if its blocked, If it hits the target then "tango is in sight".
This way you use only one ray per frame (or every 5 frames etc)

I'm understanding this in theory, but I'm not sure how to set it up.  What Action can I use to get the Vector3 Angle?

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Best way to use raycasts for line-of-sight?
« Reply #9 on: March 28, 2014, 01:59:05 PM »
vector 3 operator
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez