As of AC v1.74.0, draggable objects can be manually picked up and dropped. This script demonstrates this by allowing draggables to be picked up with a single press of the "1" key, and dropped with a press of the "2" key.
To use it:
- Create a new C# file named ForceGrab.cs and paste in the code below
- Add a GameObject to the scene and attach the Force Grab component
- Press "1" when the mouse is over a draggable object to grab it, and "2" to drop it
ForceGrab.cs:
using UnityEngine;
using AC;
[DefaultExecutionOrder (100)]
public class ForceGrab : MonoBehaviour
{
private DragBase dragBase;
private HeldObjectData heldObjectData = null;
private Vector3 lastMousePosition;
private void OnEnable ()
{
dragBase = GetComponent<DragBase> ();
EventManager.OnGrabMoveable += OnGrabMoveable;
EventManager.OnDropMoveable += OnDropMoveable;
}
private void OnDisable ()
{
EventManager.OnGrabMoveable -= OnGrabMoveable;
EventManager.OnDropMoveable -= OnDropMoveable;
}
private void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1) && heldObjectData == null)
{
dragBase.Grab (dragBase.transform.position + Vector3.one);
}
else if (Input.GetKeyDown (KeyCode.Alpha2) && heldObjectData != null)
{
dragBase.LetGo ();
}
if (heldObjectData != null)
{
Vector2 deltaDragMouse = (Input.mousePosition - lastMousePosition) / Time.deltaTime;
heldObjectData.Drag (Vector3.zero, deltaDragMouse, Input.mousePosition);
}
lastMousePosition = Input.mousePosition;
}
private void OnGrabMoveable (DragBase dragBase)
{
if (this.dragBase == dragBase)
{
heldObjectData = KickStarter.playerInput.GetHeldObjectData (dragBase);
heldObjectData.IgnoreDragState = true;
heldObjectData.IgnoreBuiltInDragInput = true;
}
}
private void OnDropMoveable (DragBase dragBase)
{
if (this.dragBase == dragBase)
{
heldObjectData = null;
}
}
}