Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 landoncr4d · Aug 03, 2015 at 07:57 PM · rigidbodyyielddelayyield waitforseconds

i need to delay an action but my code isnt working

I need to delay the action inside the OnCollisionEnter so that gravity turns on a second after my player hits the object but yield WaitForSeconds is not working. am I using it incorrectly or is there another way that this needs to be done? This is the code and I would much appreciate if someone would edit it. As of now the gravity turns on immediately when collided with but i want it to be delayed a second after collision before gravity is turned back on.

 #pragma strict
 
  
 function OnCollisionEnter(Player)
 {
       yield WaitForSeconds (2);
         rigidbody.useGravity = true;
  }
Comment
Add comment · Show 3
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 DiegoSLTS · Aug 03, 2015 at 08:47 PM 0
Share

I'm not familiar with UnityScript, but maybe OnCollisionEnter can't be called as a coroutine. Try calling a function that does the yield internally, something like:

 #pragma strict
      
 function OnCollisionEnter(Player)
 {
     WaitAndSetGravity();
 }
 
 function WaitAndSetGravity()
 {
     yield WaitForSeconds (2);
     rigidbody.useGravity = true;
 }
avatar image Positive7 · Aug 03, 2015 at 08:55 PM 0
Share

OnCollision... works fine as a Coroutine

avatar image landoncr4d · Aug 03, 2015 at 08:59 PM 0
Share

I see what you are saying and I am extremely new to coroutines and will look into this answer but this code directly does not work. But i never considered that OnCollisionEnter might not work as a coroutine so thank you

1 Reply

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

Answer by Positive7 · Aug 03, 2015 at 08:29 PM

  #pragma strict
  
   
  function OnCollisionEnter(col : Collision)
  {
         if(col.gameObject.tag == "Player"){
            yield WaitForSeconds (2);
         GetComponent.<Rigidbody>().useGravity = true;
         }
          
   }

Comment
Add comment · Show 9 · 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 landoncr4d · Aug 03, 2015 at 08:39 PM 0
Share

this does the same thing as before as it initiates gravity immediately ins$$anonymous$$d of waiting 2 seconds and I have absolutely no idea what is wrong with it but thank you for trying to help

avatar image Positive7 · Aug 03, 2015 at 08:42 PM 0
Share

$$anonymous$$aybe a stupidish question but did you turn off gravity on the object Rigidbody? I tested above script and it worked fine.

avatar image landoncr4d · Aug 03, 2015 at 08:59 PM 0
Share

yes for this object that the player is to land on, the gravity is off

avatar image Positive7 · Aug 03, 2015 at 09:12 PM 1
Share

Ohh. so it's something you jump on? Does Player uses Gravity? Is it possible it starts moving because player pushes it down..

 #pragma strict
  
  function OnCollisionEnter(col : Collision)
  {
     if(col.gameObject.tag == "Player"){
         GetComponent.<Rigidbody>().is$$anonymous$$inematic = true;
            yield WaitForSeconds (5);
            GetComponent.<Rigidbody>().is$$anonymous$$inematic = false;
         GetComponent.<Rigidbody>().useGravity = true;
     }
          
  }
avatar image landoncr4d · Aug 03, 2015 at 09:27 PM 0
Share

it is something i jump on what happens with the original code is that it starts moving as soon as i jump on it. what I want it to do is start moving two seconds after i jump on it

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Reloading delay problem.. 0 Answers

Problem with yield 0 Answers

Delay in Instantiate only runs once 2 Answers

yield WaitForSeconds 1 Answer

Calling a function in another script not working with yield 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