Forum rules - please read before posting.

How to trigger Moveable_Drag via external script and or action list.

Hello Chris! I'm wanting to trigger a Moveable_Drag object via code or action list. At the moment I am using a code that you so graciously made for me which allows for toggleable draggable which allows me to "examine objects" at a specific location set with a marker and uses a click to toggle and untoggle. I've modified it since then myself (please excuse all the gaps, not sure what happened to the formatting)

Here it is

using UnityEngine;
using System.Collections;
using AC;

public class ReturnPickUp : MonoBehaviour

{

    public Marker markerWhenHeld;

    public float moveSpeed = 10f;

    //Attach quad in front of camera to give blurred background 
    public GameObject m_Plane;

    //Attach object you want to focus on
    public GameObject m_mesh;

    private Vector3 originalPosition;

    private Quaternion originalRotation;

    private bool doReturn;

    private bool isHeld;

    private DragBase dragBase;

    private LerpUtils.Vector3Lerp positionLerp = new LerpUtils.Vector3Lerp();

    private LerpUtils.QuaternionLerp rotationLerp = new LerpUtils.QuaternionLerp();



    private void OnEnable ()

    {

        dragBase = (DragBase) GetComponent<DragBase>();


        EventManager.OnGrabMoveable += GrabMoveable;

        EventManager.OnDropMoveable += DropMoveable;

    }


    private void OnDisable ()

    {

        EventManager.OnGrabMoveable -= GrabMoveable;

        EventManager.OnDropMoveable -= DropMoveable;

    }



    private void Update()

    {

        if (doReturn)

        {

            transform.position = positionLerp.Update (transform.position, originalPosition, moveSpeed);

            transform.rotation = rotationLerp.Update (transform.rotation, originalRotation, moveSpeed);

        }

        else if (isHeld && markerWhenHeld != null)

        {

            transform.position = positionLerp.Update (transform.position, markerWhenHeld.transform.position, moveSpeed);

        }

    }





    private void GrabMoveable (DragBase _dragBase)

    {


        if (_dragBase == dragBase)

        {
            foreach (Transform child in transform)
            {
                // Changes objects layers to new layer to allow 'blurring' of background
                    child.gameObject.layer = LayerMask.NameToLayer( "Camera2" );
                    m_mesh.layer = LayerMask.NameToLayer( "Camera2" );
            }

            // Activates plane with background effect
            m_Plane.SetActive(true);

            // turns off the screen lock so you can rotate the object without having any first person influence
            KickStarter.player.freeAimLocked = true;

            originalPosition = transform.position;

            originalRotation = transform.rotation;



            doReturn = false;

            isHeld = true;

        }

    }





    private void DropMoveable (DragBase _dragBase)

    {
        //KickStarter.player.freeAimLocked = false;
        gameObject.layer = LayerMask.NameToLayer( "Default" );

        if (_dragBase == dragBase)

        {
            foreach (Transform child in transform)
            {
                    child.gameObject.layer = LayerMask.NameToLayer( "Default" );
                    m_mesh.layer = LayerMask.NameToLayer( "Default");
            }

            m_Plane.SetActive(false);

            KickStarter.player.freeAimLocked = false;

            doReturn = true;

            isHeld = false;



            if (moveSpeed <= 0f)

            {

                transform.position = originalPosition;

                transform.rotation = originalRotation;

            }

        }

    }



}

However I have no idea how to trigger it without clicking on the actual object. I tried to comb through the AC script for the Moveable_Drag, but couldn't actually figure out how its triggered. I tried sending grab() but that didn't work.

Some extra context to be super clear, I want to use this to allow the player to observe an object that is given to them, but I don't want them to have to click on it, I want the game it automatically bring it up.

If you could point me in the right direction to trigger the Moveable_Drag with code or via an action list that would be much appreciated, thank you for all your help.

Comments

  • To clarify your intent: are you looking to have the object move with the player's mouse position without the need to hold down a button, or simply present it to the player so that they can manipulate it in the normal way, i.e. with a button held?

    If the latter, it's not a case of forcing the object to be grabbed, but moving the object to a Marker placed in front of the MainCamera with Object: Transform, turning on the Moveable_Drag component with Object: Send message.

  • Apologies I am an idiot, I totally forgot there was another code required to get the toggle drag to work. It's been a while since I looked at how it worked. Here is the other key to the puzzle.

    **To clarify, I already have it working so you don't need to hold down a button, and it already transforms the object to a marker that is attached to the player camera **

    using UnityEngine;
    
    using AC;
    
    
    
    public class ToggleDrag : MonoBehaviour
    
    {
    
    
    
        public string toggleDragInput = "ToggleDrag";
    
        private bool waitForUp;
    
        private bool pressedThisFrame;
    
        private bool noToggleOff;
    
        public void Drop ()
    
        {
            KickStarter.playerInput.LetGo ();
        }
    
    
    
    
    
        private void Start ()
    
        {
    
            KickStarter.playerInput.dragOverrideInput = toggleDragInput;
    
            KickStarter.playerInput.InputGetButtonDelegate = My_InputGetButton;
    
        }
    
    
    
    
    
        private void OnEnable ()
    
        {
    
            EventManager.OnGrabMoveable += ResetInput;
    
            EventManager.OnDropMoveable += ResetInput;
    
        }
    
    
    
    
    
        private void OnDisable ()
    
        {
    
            EventManager.OnGrabMoveable -= ResetInput;
    
            EventManager.OnDropMoveable -= ResetInput;
    
        }
    
    
    
    
    
        private void LateUpdate ()
    
        {
    
            pressedThisFrame = Input.GetButtonDown (toggleDragInput);
    
        }
    
    
    
    
    
        private bool My_InputGetButton (string axisName)
    
        {
    
            try
    
            {
    
                if (axisName == toggleDragInput)
    
                {
    
                    if (!KickStarter.playerInput.IsDragObjectHeld ())
    
                    {
    
                        return pressedThisFrame;
    
                    }
    
                    else
    
                    {
    
                        if (pressedThisFrame)
    
                        {
    
                            noToggleOff = AC.GlobalVariables.GetBooleanValue (92);
                            if(noToggleOff == false)
                            {
                                KickStarter.playerInput.LetGo ();
    
                                return false;
                            }
    
    
    
    
    
                        }
    
                        return true;
    
                    }
    
                }
    
    
    
                return Input.GetButton (axisName);
    
            }
    
            catch {}
    
    
    
            return false;
    
        }
    
    
    
    
        private void ResetInput (DragBase dragBase)
    
        {
    
            pressedThisFrame = false;
    
        }
    
    
    
    }
    

    **So exactly what I am asking is ** How to turn on the Moveable_Drag component with code or an action list. I have tried Object: Send Message as you say but it does not work. I have tried both using "Turn On" and Grab. Combing through the code I cannot see what I need to send it.

    Thank you so much for your help!

  • In theory simply already turning on the Moveable_Drag component on the object is going to transform the object to a specified marker, and allow to rotate without holding down a button, exactly how it is if you click on the object itself

  • I have tried Object: Send Message as you say but it does not work.

    The "Turn On" message, passed to a Moveable object, acts in the same way it does a Hotspot - it makes it available for interaction. It won't grab the object itself. My suggestion was to have it require clicking, but only be interactive when shown to the Player.

    allow to rotate without holding down a button, exactly how it is if you click on the object itself

    If you're using custom scripting to move the object to/from the camera, and want to further things with the ability to turn it without input from the player, it may be worth using your own system entirely. At this point, you're only using AC to handle the rotation based on the mouse position, and there should be example available on Unity's forums on that.

    I agree it may be useful to provide the ability to manually grab an object without the need for overriding input, though. It's not possible at the moment, but I will look into it.

  • Ah yes its see. I probably should just bite the bullet and build my own rotation system for my specific needs. Thank you Chris!

  • I can confirm the next update will allow a custom script to override the input state of a draggable object, so that it can be dragged while no input occurs.

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.