Hello everyone. I am creating a Gone Home style game and I am using a ReturnPickUp script. It works great in all situations except if the object I want to inspect is parented to another object, like a drawer for example. The object moves with the drawer when I open it but when I close it, it stays floating in the air. I took a look inside of the ReturnPickUp script and figured out that after returning to the original position it just keeps updating it's position indefinitely, or more precisely, untill you pick up the object again. I made some changes to the script and now it will stop updating after 1 second. That seems to do the trick but it's not perfect and there are certain situations in which the object could stay stuck in the air. Since I am not that versed in scripting I wanted to ask if there is a way to disable the updating once the object returns to it's original position. I think that would solve the problem for good. Here is the script with modifications I made. There is also a gif so that you can see the problem as well. Thanks!
using UnityEngine;
using System.Collections;
using AC;
public class ReturnPickUp : MonoBehaviour
{
[SerializeField] private Marker markerWhenHeld = null;
public float moveSpeed = 10f;
private Vector3 originalPosition;
private Quaternion originalRotation;
private bool doReturn;
private bool isHeld;
private DragBase dragBase;
private Moveable_PickUp pickUp;
private LerpUtils.Vector3Lerp positionLerp = new LerpUtils.Vector3Lerp ();
private LerpUtils.QuaternionLerp rotationLerp = new LerpUtils.QuaternionLerp ();
private void OnEnable ()
{
dragBase = GetComponent<DragBase> ();
pickUp = 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);
//doReturn = false;
StartCoroutine(StopUpdating());
}
else if (isHeld && markerWhenHeld != null)
{
if (pickUp)
{
pickUp.OverrideMoveToPosition (markerWhenHeld.transform.position);
}
else
{
transform.position = positionLerp.Update (transform.position, markerWhenHeld.transform.position, moveSpeed);
}
}
}
IEnumerator StopUpdating()
{
yield return new WaitForSecondsRealtime(1);
doReturn = false;
}
private void GrabMoveable (DragBase _dragBase)
{
if (_dragBase == dragBase)
{
originalPosition = transform.position;
originalRotation = transform.rotation;
doReturn = false;
isHeld = true;
if (KickStarter.player)
{
KickStarter.player.upMovementLocked = KickStarter.player.downMovementLocked = KickStarter.player.leftMovementLocked = KickStarter.player.rightMovementLocked = true;
}
}
}
private void DropMoveable (DragBase _dragBase)
{
if (_dragBase == dragBase)
{
doReturn = true;
isHeld = false;
if (moveSpeed <= 0f)
{
transform.position = originalPosition;
transform.rotation = originalRotation;
}
if (KickStarter.player)
{
KickStarter.player.upMovementLocked = KickStarter.player.downMovementLocked = KickStarter.player.leftMovementLocked = KickStarter.player.rightMovementLocked = false;
}
}
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
You've got the StartCoroutine call in your Update function - meaning a new instance of it will be run for every frame that doReturn is true.
Move the line from there to the DropMoveable function and it should work.
Thanks for the reply Chris! Yeah running it on every frame was not the brightest of ideas But I spent the weekend trying to improve it further and I think I managed to make it half decent at least. It now checks if the object returns to it's original position and then stops updating. Also the collider is disabled as the object is returning to it's original position so now objects can't stay stuck in the air no more. Posting the script if anyone needs it for their project. Also if anyone notices anything dumb I did in the code (again) let me know. As I said, coding is not my strong suite so any feedback is welcome, an opportunity to learn
using UnityEngine;
using System.Collections;
using AC;
public class ReturnPickupNew : MonoBehaviour
{
}