Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by CSXsonic · Sep 18, 2015 at 01:14 PM · c#transformtime

How do I make something move for only a few seconds C#

So I'm getting more and more experienced in C# but I hit a small roadblock. I am making a claw game and I wanted to have this script which made the claw move down and then back up after you press space. So far, I have the script for it to move

 using UnityEngine;
 using System.Collections;
 
 public class lift : MonoBehaviour {
 
     void Update () {
         if(Input.GetKey("space"))
         {
             transform.Translate(Vector3.down * Time.deltaTime);
         }
     }
 
 
 
         
     }
 

What I want to know is if there is a command for something to happen for only a period of time. In this case, I want the claw to move down for only about a second. Thanks in advance.

Comment
Add comment · Show 2
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 Runalotski · Sep 18, 2015 at 05:49 PM 0
Share

Corutines are used for making things happen over a period of time so maybe something useful there i have not used it in this way before though. but you can use corutines to make a texture flash red or something to show damage so should be useful for this.

avatar image Runalotski Runalotski · Sep 18, 2015 at 05:53 PM 0
Share

Or actually using a lerp method would be better as you can set the target position you want to reach and how long you want to tak to get there the is vector.lerp and mathf.lerp this would be good as you will not overshoot your target position if that is important

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Sep 18, 2015 at 06:44 PM

You hace to keep track of how much time the code has run, and only call it while the elapsed time is less than the duration you want. There's nothing you can write in a single line that would do it.

It's really simple anyway, the simplest code I can think of, without a coroutine, would be something like this:

 using UnityEngine;
 
 public class SomeScript : MonoBehaviour {
 
     public float duration; //set the duration in the inspector
     float elapsedTime = Mathf.Infinite;
 
     void Update() {
         if (Input.GetKeyDown(KeyCode.Space)) {
             elapsedTime = 0f;
         }
 
         if (elapsedTime < duration) {
             transform.Translate(Vector3.down*Time.deltaTime);
             elapsedTime += Time.deltaTime;
         }
     }
 }

A coroutine removes the need of class members and have other advantages, but adds some complexity. Something like this should work too:

 using UnityEngine;
 
 public class SomeScript : MonoBehaviour {
 
     public float duration; //set the duration in the inspector
    
     void Update() {
         if (Input.GetKeyDown(KeyCode.Space)) {
             StartCoroutine(MoveDown());
         }
     }

     IEnumerator MoveDown() {
         float elapsedTime = 0f;
         while (elapsedTime < duration) {
             transform.Translate(Vector3.down*Time.deltaTime);
             elapsedTime += Time.deltaTime;
             yield return null;
         }
     }
 }
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

28 People are following this question.

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

Related Questions

I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer

Time.time doesn't show up? 2 Answers

Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer

Trying to move buttons via Coroutine and transform.Translate 0 Answers

why the camera rotate in the z axis 0 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