playMaker

Author Topic: Raycast Help  (Read 2482 times)

Natecurt

  • Playmaker Newbie
  • *
  • Posts: 8
Raycast Help
« on: August 16, 2018, 06:25:03 PM »
Hi everyone,

I'm quite new to PlayMaker and was hoping someone could help me figure out how to get my raycast functioning the way I'd like.

Currently I have game objects in my scene which are tagged. When the raycast detects these objects it deactivates them until it detects a new object. Once the new object is detected it is deactivated and the previously deactivated object is re-enabled.

The issue is that if there are multiple objects in the raycast's path with the proper tags it will repeatedly flicker between them enabling and disabling them.

How can I have the raycast only detect the closest game object? Would I need to use raycast all? I haven't yet worked with arrays so I am not sure how this would work.

Any help would be appreciated!

Thanks,
Nate

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Raycast Help
« Reply #1 on: August 17, 2018, 12:33:32 AM »
Hi.
From what i understand it does only detects the closest, but when the object is disabled it will not be detected anymore and the the one behind is detected, enabling the 1st object again which then gets detected again by the ray cast.

So the raycast does work correctly but you might need to rethink your logic by (for example) using a delay or temporary changing tags

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Raycast Help
« Reply #2 on: August 17, 2018, 05:15:39 AM »
Djaydino is correct, you'll need to rethink your flow of logic.

You should also consider how you disable the object, you may be able to simply disable the object's mesh renderer, though if you require the collider to be disabled too then you could consider additionally changing the Layer the object is on during the process simultaneously (if you do this you can disable collisions between the relevant layers in the layer collision matrix).

Natecurt

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Raycast Help
« Reply #3 on: August 17, 2018, 06:22:17 PM »
You should also consider how you disable the object, you may be able to simply disable the object's mesh renderer.

Thanks for the reply,

I appreciate the feedback and have reconsidered my logic a bit. Here is what I am trying to accomplish and how I think it could be done:

I have multiple walls in-front of a camera which has a raycast attached to it. The raycast should ideally detect both walls and hide them while they are detected. As soon as the raycast moves off the the objects they reappear.

Therefore, would it be best to do a Raycast All so that I can store all the objects in an array and somehow hide the mesh renderers of everything in that array? If that's not possible maybe the walls detected could be tagged as something else while they are in view of the raycast so that I can somehow hide the mesh renderer of everything with that tag?

I'm not sure how this would be done but will play around with playmaker in the meantime. Looking forward to your replies!
« Last Edit: August 17, 2018, 06:49:22 PM by Natecurt »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Raycast Help
« Reply #4 on: August 18, 2018, 05:43:33 AM »
Hi.
Maybe you can use some triggers (set as child on the camera) instead of raycast
or use 2 raycasts.

If you could show us some images/video on how it should work it will be easier to determine what solution will be good in your case.

Natecurt

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Raycast Help
« Reply #5 on: August 21, 2018, 07:45:31 PM »
Hi again,

Thanks for the reply. I found that using layers was actually the simplest solution.

This is what I did:

I created two raycasts, Raycast 1 and Raycast 2. Raycast 1 searches for gameobjects under the layer "Hide", and Raycast 2 searches for gameobjects under the layer "Unhide".

I placed a collider close to the player and set it as a trigger under the layer "Unhide". This way whenever the collider is in view of Raycast 2 it means the player is visible with no other colliders inbetween the player and the camera. If this is the case, a Set Visibility action is used to re-enable any hidden meshes.

When an object with the layer "Hide" comes between the "Unhide" collider and camera, Raycast 1 detects it and disables it's mesh renderer using the Set Visibility action, which connects back to the initial Raycast state. I disabled reset on exist under this action as well as well.

I do have one problem however that I am not certain on how to correct:

Since Raycast 1 only stores the most recently seen object under the layer "Hide", if the raycast moves from one object to another and then off of both, only the most recent object will be re-enabled as the stored variable has been overwritten by the newest object.

Does anyone have suggestions on how I may solve this?

Thanks!

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Raycast Help
« Reply #6 on: August 22, 2018, 05:28:39 AM »
Nice solution.

You could add each object to an array as it is hidden, then remove each object from the array when unhidden. At the point the player is visible you can loop through the remaining objects stored in the array, turning each mesh renderer back on as you go.

Natecurt

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Raycast Help
« Reply #7 on: August 22, 2018, 07:48:58 PM »
Nice solution.

You could add each object to an array as it is hidden, then remove each object from the array when unhidden. At the point the player is visible you can loop through the remaining objects stored in the array, turning each mesh renderer back on as you go.

Thanks for the suggestion Doh,

I messed around with arrays for quite a while but couldn't get them to work the way I needed(probably due to my own lack of knowledge however).

Although I did find another solution to my issue. This is how the final product works:

Two raycasts shoot towards a player. Raycast 1 detects objects with the layer "Hide". Raycast 2 detects objects with the layer "Unhide".

When Raycast 2 detects a collider attached to the player with the layer "Unhide" any previously seen wall has it's visibility set to true.

When Raycast 1 detects a game object with the layer "Hide" it runs a check. To start this check I first add the value 1 to an int variable. Then I compare the int. If it has a value of 1, this must be the first object it has seen. Therefore, set the game object as variable "Wall 1" and set it's visibility as false. Then loop back into the raycasts.

If an int greater than 1 is found, set the game object as variable "Wall 2". Then set the visibility of "Wall 1" to true and the visibility of "Wall 2" to false. After this, replace "Wall 1" with the game object used in "Wall 2" and set the int variable back to 1. Then loop back to the raycasts again.

In short, when the raycasts move directly from one wall to another they will only hide the wall/object which is directly infront of the player while making sure to unhide and previously hidden walls the player was behind before.

Thanks again for the help guys! I hope this is of some use to others.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Raycast Help
« Reply #8 on: August 23, 2018, 08:20:51 AM »
Hi.
Thanks for sharing your solution.