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 jeremy · May 31, 2012 at 07:36 PM · gameobjecttransformtime

How to delay a line of code.

Hi I need to know how to delay a line of code and heres the code im using its in java. So heres what I need, I need to delay the transform.Translate for about five seconds, so if you can do this for me it would be so much help. Thanks

var speed : float = 0;

function Update () {

transform.Translate(Vector3(0,0,speed) * Time.deltaTime);

  }
Comment
Add comment · Show 1
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 GC1983 · May 31, 2012 at 07:47 PM 0
Share

Well, it would depend on how you are triggering it. You could set the translate into a boolean, so when you collide or do something else, you could call that boolean to be false which will cut off the translate. And within the function that called the boolean to be false, you could put a 5 second timer on that with deltaTime or WaitForSeconds() to reset the boolean back to true.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · May 31, 2012 at 07:50 PM

EDIT Per Wolfram's point - my first answer was not a good plan :)

However, it strikes me that perhaps you want the speed increase to be delayed by 5 seconds - and then ramp up as it ramped over that period. In which case you need a rollling buffer of the speed value:

  var speeds  = new List.<float>();

  function Update() {
      speeds.Add(speed);
      if(Time.time >= 5) {
          transform.Translate(Vector3(0,0,speeds[0]) * Time.deltaTime);
          speeds.RemoveAt(0);
      }
 }
Comment
Add comment · Show 4 · 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 whydoidoit · May 31, 2012 at 07:52 PM 0
Share

I wondering what effect you are try to achieve though - feels to me that there might be a better way...

avatar image Wolfram · May 31, 2012 at 09:26 PM 0
Share

Putting Invoke() into Update() is a resource hog, spawning a delayed call to $$anonymous$$ove() each frame. So for the 5 seconds delay and a framerate of 60fps, 300 coroutines are spawned, before even the first one is actually executed. Invoke() is better suited for delayed events that are triggered only once.

avatar image whydoidoit · Jun 01, 2012 at 02:15 PM 0
Share

Oh I agree - and hence my comment - I was presu$$anonymous$$g some kind of snake follow animation - but on looking at the code again, it probably isn't right.

avatar image whydoidoit · Jun 01, 2012 at 02:23 PM 0
Share

Now that's what I was trying to do with my first answer - but totally missed it ;(

avatar image
1

Answer by Wolfram · May 31, 2012 at 09:33 PM

Assuming by "delay" you merely mean "I don't want it to be executed during the first 5 seconds", you can do something like this:

 function Update () {
     if(Time.time>=5)
         transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
 }

Time.time contains the total time since your game started. If you want to measure the time from a different point (for example, if your Update() is part of an object that is being instantiated), you can store the value in Start() with startTime=Time.time, and then use the difference (Time.time-startTime) in Update.

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

enable scripts/ components on other gameobjects for a different gameobject? 1 Answer

How to destroy a gameobject on collision 2 Answers

How to emit a sound on specific collision. 1 Answer

How to delay a shot. 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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