FANDOM


This script returns a PickUp object to its original position and rotation upon release. The movement speed can be adjusted, and will snap back insrtantly if set to zero.

To use, place this code in a script named ReturnPickUp and attach to the PickUp you want to affect.

This will also work with Draggable objects with a "Drag type" set to "Rotate Only".

You can also optionally supply a "Marker When Held" that the object will move to while it is held.

using UnityEngine;
using System.Collections;
using AC;

public class ReturnPickUp : MonoBehaviour
{

public Marker markerWhenHeld;
public float moveSpeed = 10f;

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)
{
originalPosition = transform.position;
originalRotation = transform.rotation;

doReturn = false;
isHeld = true;
}
}


private void DropMoveable (DragBase _dragBase)
{
if (_dragBase == dragBase)
{
doReturn = true;
isHeld = false;

if (moveSpeed <= 0f)
{
transform.position = originalPosition;
transform.rotation = originalRotation;
}
}
}

}
Community content is available under CC-BY-SA unless otherwise noted.