Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SirGive · Oct 06, 2010 at 09:05 AM · delaypickupuppick

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. :|

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirGive · Oct 07, 2010 at 01:36 AM 0
Share

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

avatar image SirGive · Oct 07, 2010 at 01:38 AM 0
Share

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.

avatar image SirGive · Oct 07, 2010 at 08:07 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges