Clicking on a Model
Whether you're making a toy or a game, you'll want to be able to capture mouse clicks and do something as a result.
Overview
In this example, we're going to look at how to detect when the player has clicked on a specific model so that we can trigger an animation. Although we'll be playing an animation when the model is clicked, once you understand how to detect that click you can trigger anything you'd like, no just animations!
Related Information
If you're unfamiliar with visual scripting, consider reading unbound's Logic Block Basics before continuing.
If you'd like to follow along or see the finished product you can download the file here.
Logic Blocks Used in This Example
- modelClick
- animation
Capturing the Click with modelClick
From the play tab, open the logic library then drag the modelClick logic block into your scene. Then, connect the model input to the model we wish to make clickable. When you’re done, your scene will look something like this:
Connecting the Animation
Next we’ll connect our animation block to the leftPressed output of modelClick to the triggerPlay input of our animation. We’re using leftPressed because it will send a true state for one frame when the player clicks while triggerPlay looks for a true state for one frame to initiate the animation. This allows us to trigger the animation once on click, play through to conclusion, and stop.
Tip
If, on the other hand, we wanted an animation to play as long as a given condition is true (such as while the mouse was hovering over the model) we would instead connect the mouseHovering output on modelClick to the play input of the animation. This would cause the animation to resume play when hovering and stop in place when not, much like toggling pause/play on a looping video.
Related Information
For more information on how to create animations in Unbound, check out the Creating Animations in Unbound article.
With the animation created and everything wired up correctly, it’ll look something like this:
And with that, you can test your game by clicking the play button and then enable input (by clicking the keyboard icon to the right of the play button) so the game will respond to clicks.
Now when the player clicks on the flower the animation plays!