Forum rules - please read before posting.

2D Draggable, trigger event on 'goal'

edited October 2019 in Technical Q&A

Hello there,

So I'm making a 2D game and have created a door bolt, allowing the player to horizontally click and hold the bolt, slide it along a straight track to lock the door. All very easy to do in AC, great plug in.

The problem that's occurred to me however is 'how can I tell AC that the player has slid the slider sprite all the way into the correct position?'

I've made a Trigger 2D box on the far right to the bolt, set it to 'Is Trigger', set trigger type to on enter, Detection Method: Rigid body collision, and I have given both the sprite and the 2D trigger a rigid body.
The idea is that on entering this zone, an action list would trigger which consists of triggering a one shot audio clip of a locking sound, removal of the bolt object, hinge track, cut to next scene etc.

However none of this worked.

Surely draggable objects have an easy way of telling AC that the player has placed the draggable object into the 'goal' position without custom scripting?

Comments

  • edited October 2019

    Draggable objects are 3D only - they won't respond to 2D Rigidbody or Collider components. You would have to rely on 3D components for the Draggable object to react to it.

    You can alternatively define an Interaction on move in the Draggable's Inspector, which is an ActionList that gets run every frame that it is held by the player. This ActionList could begin with a Moveable: Check track position Action, to work out if it's close enough to the end (e.g. > 0.9) and run a new Cutscene to play the animation if so. Just be sure to set this Interaction's When running property to Run In Background, so that it doesn't interfere with gameplay.

    AC v1.69.0 also introduced "track snapping", whereby you can define snap points along tracks. When a Draggable enters such a snap point's region, it will automatically move to that point upon release, and you can also hook into the OnDraggableSnap custom event to run a new Cutscene at this moment, i.e.:

    using UnityEngine;
    using AC;
    
    public class CutsceneOnSnap : MonoBehaviour
    {
    
        public Cutscene cutsceneOnSnap;
        public int snapIDToReactTo = 0;
    
        private void OnEnable ()
        {
            EventManager.OnDraggableSnap += OnDraggableSnap;
        }
    
        private void OnDisable ()
        {
            EventManager.OnDraggableSnap -= OnDraggableSnap;
        }
    
    
        private void OnDraggableSnap (DragBase dragBase, DragTrack track, TrackSnapData trackSnapData)
        {
            if (dragBase == GetComponent <DragBase>() && trackSnapData.ID == snapIDToReactTo)
            {
                cutsceneOnSnap.Interact ();
            }
        }
    
    }
    

    (Paste in a C# script file named CutsceneOnSnap.cs, attach to the Draggable object and fill in it's Inspector).

  • Oh wow, really solid advice and insight here. Thank you for the reply.

    The check track position function works a charm. Some fine tuning needed but it's pretty much there. Appreciate the script too as I know this is something I'm going too need too at some point!

    Thanks again

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.