Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction Ray + Shapecasting for easier object interaction #281

Merged
merged 4 commits into from
Sep 22, 2024

Conversation

OvercastInteractive
Copy link

  • Uses the existing raycasting behavior for when aiming directly at the target interactable
  • If not aiming directly at the target interactable, a shapecast is used to detect any nearby interactables
  • If raycasting a body that is not interactable, such as a tabletop, moves a 'hotspot' node to the raycasted point
  • The hotspot point is used as a reference for any shapecast hit colliders to compare distances with all other hits
  • The closest interactable collider to the hotspot is selected as the target interactable

…hters to debug its behavior

This commit modifies the existing InteractionRaycast.gd and to a lesser extent the PlayerInteractionComponent.gd.

InteractionRaycast now uses a shapecast when the raycast doesn't hit a valid collider. If the shapecast hits multiple valid colliders it will use a 'hotspot' and judge distances between it and valid colliders to determine which object the player is more likely intending to pick up.

There is a known issue regarding the ShapeCast3D node reference being lost on scene change.
@Phazorknight
Copy link
Owner

Hey,thanks so much for this, this is quite simple but yields amazing results!

I get a weird issue though:
In the Lobby level scene, everything works as expected, but in the Laboratory scene, the interaction_raycast.gd throws an error. Seems like the hotspot and highlighter reference are all null, which baffles me, as they're set onready

This happens whether I start directly in the Laboratory scene or transition to it from the Lobby scene.
I've tried to copy the Player.tscn from the Lobby to the Lab to see if there are any weird overrides, but the error persists. ☹️

@OvercastInteractive
Copy link
Author

I expected the highlighters to be removed after a little more testing (as I found looking into cabinets and drawers toward multiple carryables produced unexpected/undesired detection results) and the hotspot can be converted into a Vector3 or instantiated on _ready(). But you're right, when I enter my own scenes from the Lobby I don't have this issue. Only when going into the Laboratory. In the Laboratory, when toggling editable children to true they appear nested under the InteractionRaycast node, as expected.

@OvercastInteractive
Copy link
Author

In order to get the Laboratory scene to work I had to add null checks and assignments for every null reference at the top of _update_interactable() and also check if each was null before updating their visibility and position, like this:

if hotspot == null:
hotspot = $ShapecastHotspot

if hotspot != null:
hotspot.transform.origin = to_local(hotspot_global_position)

It's like their references are lost or cleared before every call. 🤨

@OvercastInteractive
Copy link
Author

The issues are due to the Enemy. I didn't realize they were using the same code, sorry about that! Here's the error code: E 0:00:19:0588 interaction_raycast.gd:8 @ _ready(): Node not found: "InteractionShapecast" (relative to "/root/Cogito05DemoLaboratory/CogitoEnemy/InteractionRaycast").

- Turned the Interaction Raycast into a packed scene, to keep child node structure consistent. This should fix reference issues with the Enemy using an interaction raycast.
- Updated the player and enemy packed scene to use said interaction raycast packed scene.
- Added the option to turn debug shapes on/off.
@Phazorknight
Copy link
Owner

Aaah, the enemy is pretty new so I totally forgot about this!
I've just pushed an update that should fix all of this (just see the description).

Take it for a spin, but I'd wager this is now ready to merge.

- Turn debug shapes off on ready if they're disabled and ensure they don't turn on any point while playing
- Removed a null ref check having to do with the ShapeCast3D not found for enemy interaction raycast on scene change
@OvercastInteractive
Copy link
Author

I made a couple of minor changes, but yeah, everything now seems to work well!

@Phazorknight
Copy link
Owner

Awesome! Again, thanks so much for your work on this. You implemented this way better than I could have.

@Phazorknight Phazorknight merged commit a8fdbac into Phazorknight:phazor_wip Sep 22, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants