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.

Note: This script requires AC v1.57 or later to work.

using UnityEngine;
using System.Collections;
using AC;

[RequireComponent (typeof (Moveable_PickUp))]
public class ReturnPickUp : MonoBehaviour
{

    public float moveSpeed = 10f;

    private Vector3 originalPosition;
    private Quaternion originalRotation;
    private bool doReturn;
    private DragBase pickUp;

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


    private void OnEnable ()
    {
        pickUp = (DragBase) GetComponent <Moveable_PickUp>();

        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);
        }
    }

    
    private void GrabMoveable (DragBase dragBase)
    {
        if (dragBase == pickUp)
        {
            originalPosition = transform.position;
            originalRotation = transform.rotation;

            doReturn = false;
        }
    }
    
    
    private void DropMoveable (DragBase dragBase)
    {
        if (dragBase == pickUp)
        {
            doReturn = true;

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

}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.