playMaker

Author Topic: Raycast hit to interactable objects only  (Read 1436 times)

VectorF22

  • Junior Playmaker
  • **
  • Posts: 50
    • View Profile
Raycast hit to interactable objects only
« on: July 13, 2016, 01:24:58 AM »
This is a really simple problem but it's causing a bit of a headache right now...

I'm working on an FPS and the idea is to have objects that you can interact with in the scene when you're facing them (like a switch). Setting the raycast to only interact with objects on a specific layer works great, up until you face the same object through a wall and it still triggers.

Is is possible in playmaker to make it only trigger the object when it is the first one that the raycast hits? I.E not go through walls/colliders?

I tried making a second raycast that gets the distance to the closest wall/collider and sets that value to the distance of the first raycast (the one that's checking for the interactable objects) but it just ends up creating an infinite loop error.

Thanks!

Zeldag

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Re: Raycast hit to interactable objects only
« Reply #1 on: July 13, 2016, 09:56:27 AM »
Hi,

From my understanding using layers can help with performance, but as you don't mind using a 2nd raycast to fix your issue, I will assume that this is not your goal.

So then I can suggest that you could just remove the intractable objects from the layer and put a unique tag on them instead. Then in the raycast settings you could save the object and use 'game object compare tag' action.  https://hutonggames.fogbugz.com/default.asp?W218

I would set it up like this: FSM 1: just have one state that sends the raycast and saves the hit game object to a global variable.

FSM2: state1: compare tag of the global game on object variable that you set in FSM1. If it is interactable go to state2
State2: compare tag every frame (and send back to state 1 if the object is no longer intractable). And add whatever actions you have for interacting.

You could also do it differently and simply add the walls to the same layer as intractable objects, while still tagging the interactable objects, then just use a tag compare to distinguish between walls and interactable objects.

This is all assuming that you haven't tagged or layered any of the objects already. But some version of this may work for you, just might need to play around with it.

Hope this helps and good luck with it!

VectorF22

  • Junior Playmaker
  • **
  • Posts: 50
    • View Profile
Re: Raycast hit to interactable objects only
« Reply #2 on: July 14, 2016, 07:33:19 AM »
That sounds like it's a good potential solution thanks! I didn't think to use a tag compare.

The way I solved it was far more expensive, but it works really well...

I have a raycast firing forward of the camera and a capsule trigger that is set to the hit position. If the distance of the raycast is low enough (meaning the player is relatively close to something) then the capsule trigger will activate. I have other triggers on points of interest (like a switch) that trigger when the capsule trigger has entered it. You can then press a key to use the switch.

It's a shame that the single raycast wasn't working... I'm still not sure why.