- Home /
Creating a small delay for a Pick up/drop script.
Alright, so I'm having a delay problem with a pickup script. Well rather, I can't get a delay working. I've looked through a bunch of delay answers on here, but I can't get any of them to work. Here is a snippet of what I'm working with:
function Update () { if( timer > seconds.getSecondsFloat() +3) if (Input.GetKeyDown("f") && itemHeld == true) { object.transform.parent = null; object = null; held = false; }
}
function OnTriggerStay(collider : Collider){ if (Input.GetKeyDown("f") && collider.tag == "hands") if (itemHeld == false){ Debug.Log("i see you keep hitting me, there"); aquireObject(); } } function OnTriggerEnter(collider : Collider){ if (Input.GetKeyDown("f") && collider.tag == "hands") if (itemHeld == false){ Debug.Log("i see you hit me, there"); aquireObject(); } }
It works, to pick up the object. But the command executes so fast that when I go to set the object down it will pick it back up. I've tried setting a counter that increments on every frame and I put in an if condition for if (counter > 60), but then I can't even pick up the object. I've tried a coroutine, but to be honest I can't find too much information on them. I could have done it wrong. I had startcoroutine(functionthatyields) and yielded 3 seconds. I called it inside the if condition an everything. But I'm not sure how to get this working. :|
Answer by skovacs1 · Oct 06, 2010 at 04:41 PM
You left out the code for dropping and any of your timer work. I'm not really sure what you're doing with the trigger either. If you're not going to include the meaningful portions of your code, it's much harder to know what you've done wrong.
The general way to do this is with a lock, be it a boolean or a timer.
BooleanLock.js
var busy : boolean = false; //Lock
function Update() { if(!busy && something) DoThing(); }
function DoThing() { busy = true; //get the lock //do stuff... yield WaitForSeconds (waitTime); //If you want to impose some wait time busy = false; //release the lock }
TimerLock.js
var busyTimer : float = 0.0; //Lock
function Update() { if(busyTimer > 0) busyTimer -= Time.deltaTime; //Releasing the lock if(busyTimer <= 0 && something) DoThing(); }
function DoThing() { busyTimer = waitTime; //get the lock //do stuff... }
If you really want to wait for your function to finish, rather than use a lock, you can yield the start of the coroutine;
WaitToFinish.js
function Update() { if(something) yield StartCoroutine(DoThing()); }
function DoThing() { //do stuff... yield WaitForSeconds (waitTime); }
I didn't include aquireObject() code because the drop function was in the Update(), and my timer for that function works. It sets timer = seconds based on system clock (the GUI consists of a real time clock) after parenting the object it collided with. The if condition in the update waits until 3 seconds go by, then it allows the object to be dropped. Which is what object.transform.parent = null and object = null do. It sets it held = false, but the the object is dropped right where it is held and the trigger command is re-entered. I just needed a delay that provides enough of a gap between
the two functions. It looks like this will work. I will implement it once I've finished coding the law of conservation of mass into C++ (which might take a while because i'm stumped) that is due at in a few hours. :P Thank you very much.
The fix was yield WaitForSeconds (1) before setting the itemHeld to false and true. :D for some reason, the time I had wasn't working. I couldn't get that yield function to for for anything. Thank you very much.
Your answer
Follow this Question
Related Questions
Object pick up script 1 Answer
Picking up an object 2 Answers
Pick Up Coins 1 Answer
Picking up object occasionally causes player to fly through air 0 Answers
Mouse locked in center 1 Answer