I must be missing something basic because I'm sure this must be easy, but I can't figure it out after a couple of hours - and searching for 'on click' here on the forum or in the manual isn't helping me I'm afraid.
I've downloaded '2DBarkExample_2018-05-06.unitypackage' and '2DClickTriggerExample_Bubble_ProximitySelector.unitypackage' both of which I think might be really helpful, but I can't get either of those examples to work with v2.0.9 (even after running the 1>2 update tool, and I can't see anything in the way they're set up to help me figure it out).
I'm making a super basic 2D RPG. I have my player on screen, and an NPC. I want to be able to click (or tap, if I'm on mobile) on an NPC to trigger a conversation. It doesn't need proximity or collision triggers which I think I can get working, I just want to click (or tap) on an NPC GameObject and have that start the dialogue. As I wander around the world I want to be able to click on different NPCs to start different dialogues, but I can't find anything in the Trigger events or any scripts to make this happen.
Like I said, I must be being stupid because this can't be hard (can it?) I just can't figure it out.
And I appreciate that it's nearly Christmas so I expect support and replies may be slow
Still, any help would be super appreciated. Thanks!
How to start a conversation 'on click' (or 'on tap' if it's mobile)?
Re: How to start a conversation 'on click' (or 'on tap' if it's mobile)?
Hi,
Try these steps:
1. Add a Selector component to the player or the camera. Set:
2. On your NPC:
The Selector/Usable/collider stuff isn't absolutely required. If your project already has some other kind of interaction system, you can certainly use that instead. Just configure it to run the Dialogue System Trigger's OnUse() method.
I ran 2DClickTriggerExample_Bubble_ProximitySelector.unitypackage through the 1.x to 2.x updater and posted the updated version on the Extras page.
You can also download it here: 2DClickTriggerExample_Bubble_ProximitySelector_2018-12-23.unitypackage
Try these steps:
1. Add a Selector component to the player or the camera. Set:
- Select At: Mouse Position
- Max Selection Distance: 999
- Run Raycasts: In 2D
- Use Key: mouse 0
2. On your NPC:
- Add a collider, such as a Box Collider 2D. This allows the Selector's raycast to detect the NPC.
- Add a Usable component. Set Max Use Distance to 999. This allows the NPC to be used if the camera no more than than 999 units away.
- Add a Dialogue System Trigger component. Select Add Action > Start Conversation, and select your conversation from the dropdown. When the NPC is used, this Dialogue System Trigger will start the conversation.
The Selector/Usable/collider stuff isn't absolutely required. If your project already has some other kind of interaction system, you can certainly use that instead. Just configure it to run the Dialogue System Trigger's OnUse() method.
I ran 2DClickTriggerExample_Bubble_ProximitySelector.unitypackage through the 1.x to 2.x updater and posted the updated version on the Extras page.
You can also download it here: 2DClickTriggerExample_Bubble_ProximitySelector_2018-12-23.unitypackage
Re: How to start a conversation 'on click' (or 'on tap' if it's mobile)?
Thanks so much for the amazingly quick reply Tony. I am going to try all this now and see how it goes.
Re: How to start a conversation 'on click' (or 'on tap' if it's mobile)?
Thanks again! I've got it working in a new test scene now thanks to your help Tony, but I still needed one extra step which I'd missed before.
I had not realised the requirement to "Enable Physics2D Support in Unity 2018+", but once I'd followed those steps (Tools → Pixel Crushers → Common → Misc → Enable Physics2D Support) it works as expected. I'd read the getting started guide a few times but had missed that note about enabling 2D support seperately until my last read-through. My mistake. It also explains why non of the 2D downloadable examples were working for me.
-
Unfortunately, in the actual game I'm making (not my test scene), I still have an issue.
My game used 2D sprites as the characters (player and NPCs) and 2D physics for some interactions between them, but the world they move through is 3D. To add perspective to the scenes as the camera follows the player, I use the perspective camera instead of the orthographic camera. It looks good, but...
Apparently there is a way to do this in Unity (https://forum.unity.com/threads/raycast ... ra.428461/ and https://docs.unity3d.com/ScriptReferenc ... onAll.html) but I'm not sure how I'd get that to work alongside the Raycasts the selector component fires out?
I always have the option to recreate the game using the 3D physics engine rather than the 2D engine (in which case I'd just use 3D colliders on the sprites, and then the perspective camera raycasts will work perfectly), which won't be too much of a problem, but if there's an easy-ish fix for the way it currently is (2D physics with a perspective camera), that would be great
I had not realised the requirement to "Enable Physics2D Support in Unity 2018+", but once I'd followed those steps (Tools → Pixel Crushers → Common → Misc → Enable Physics2D Support) it works as expected. I'd read the getting started guide a few times but had missed that note about enabling 2D support seperately until my last read-through. My mistake. It also explains why non of the 2D downloadable examples were working for me.
-
Unfortunately, in the actual game I'm making (not my test scene), I still have an issue.
My game used 2D sprites as the characters (player and NPCs) and 2D physics for some interactions between them, but the world they move through is 3D. To add perspective to the scenes as the camera follows the player, I use the perspective camera instead of the orthographic camera. It looks good, but...
- The selector component allow me to Run Raycasts in 2D, or Run Raycasts in 3D.
- If I run raycasts in 3D, I need to do that with a perspective camera, and then the 2D colliders on the sprites aren't clickable
- if I run raycasts in 2D, I need to use the orthographic camera. This works and means the sprites are now clickable, but I've lost the perspective on 3D objects in the scene
Apparently there is a way to do this in Unity (https://forum.unity.com/threads/raycast ... ra.428461/ and https://docs.unity3d.com/ScriptReferenc ... onAll.html) but I'm not sure how I'd get that to work alongside the Raycasts the selector component fires out?
I always have the option to recreate the game using the 3D physics engine rather than the 2D engine (in which case I'd just use 3D colliders on the sprites, and then the perspective camera raycasts will work perfectly), which won't be too much of a problem, but if there's an easy-ish fix for the way it currently is (2D physics with a perspective camera), that would be great
Re: How to start a conversation 'on click' (or 'on tap' if it's mobile)?
Hi,
If you don't mind doing a little scripting, Selector.Run2DRaycast is overridable. You can make a subclass that overrides this method to use Physics2D.GetRayIntersection or Physics2D.GetRayInteractionNonAlloc instead of Physics2D.RaycastNonAlloc.
If you don't mind doing a little scripting, Selector.Run2DRaycast is overridable. You can make a subclass that overrides this method to use Physics2D.GetRayIntersection or Physics2D.GetRayInteractionNonAlloc instead of Physics2D.RaycastNonAlloc.