playMaker

Author Topic: How to create a Raycast Line of Sight system?  (Read 2046 times)

Adam Z

  • Full Member
  • ***
  • Posts: 207
How to create a Raycast Line of Sight system?
« on: October 25, 2016, 11:37:31 AM »
After previous discussions months back, it probably was a good idea to detect if the NPC is close enough to the player AND looking in their field of view before sending a Raycast. If so, then shoot a Ray at the player to see if there are any obstacles in the way. Sebaslive suggested:

"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!"

My Raycast works fine by creating an empty Game Object, parenting that to NPC, and adding a Look At, Raycast, and Game Object Compare Tag to determine whether the Ray hit the Player or another object such as a wall, then sending the result to an Event.

The problem I'm having is figuring out the Line of Sight like Sebaslive suggested.  I have a state prior to the Raycast State that Gets Position of both the NPC and Player. That gets inputted into the Vector3 Operator. I set the Operation to Angle, but I'm not sure what I need to do next. I can use a Float Compare, but that will still allow for detection if the Player is behind the NPC.  Maybe there's a better way to do it?

Any help would be appreciated. Thanks!

« Last Edit: October 25, 2016, 11:45:58 AM by Adam Z »

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: How to create a Raycast Line of Sight system?
« Reply #1 on: October 25, 2016, 11:02:52 PM »
There are Get angle and get signed angle tasks on Ecosystem that should help you.
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!