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 kumarc123 · May 02, 2014 at 06:01 AM · timemovingtimestep

How to perform a task in a specified time

Hello friends,

I want to make some utility classes in C# to perform the following tasks.

  1. To make an object take same amount of time in moving an object from one position to another position. For example the object was at pos (0, 0, 0) and I want to move to pos (0, 0, 100) in exactly 5 secs. If change the target position to (0, 0, 1000) then also it should take the same amount of time (5 secs). If change the time instead of target position, suppose time 20 secs for target position (0, 0, 100) then i must complete the task in 20 secs instead of 5 secs.

  2. As stated above you can think of rotating an object to some degrees in 5 secs, rotating an object exactly 100 times in 10 secs etc.

I am a bit confused of using Time.deltatime and Time.time in thse scenarios. Please provide your suggestions if you have.

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 robertbu · May 02, 2014 at 06:12 AM 0
Share

You might study this for movement and rotation: http://wiki.unity3d.com/index.php?title=$$anonymous$$oveObject

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Deign · May 02, 2014 at 06:21 AM

You can use Time.deltaTime to find out the amount of time since the most recent frame. Using this you can compute the amount of distance that the object should travel. Think of it like miles per hour. If you want an object to move from (0, 0, 0) to (0, 0, 100) in 5 seconds, you could also write that as moving (0, 0, 20) per second for 5 seconds. And so you can use the following code to get the results you're looking for.

 void MoveObject(Transform trans, Vector3 direction, float speed)
 {
     trans.position += direction.normalized * speed * Time.deltaTime;
 }

To get the inputs for this function, of course the Transform comes from the object you're moving. As for the direction, this can be obtained by doing the following:

 Vector3 direction = locationToGoTo - transform.position;

And lastly, the speed would be this:

 float speed = direction.magnitude / timeToGetThere;
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
avatar image
0

Answer by jamiltron · May 02, 2014 at 06:16 AM

Look into using Vector3.Lerp in a Coroutine.

Essentially you will record your to and from position, calculate your overall rate as a percentage, and then continually upDate a rate to be the sum of itself plus Time.deltaTime * yourRate, and update the position to be:

 transform.position = Vector3.Lerp(fromPosition, toPosition, totalSumOfRates);

Until the totalSum... is equal to or greater than 1 (100%).

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

22 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

Related Questions

Fixed timestep... 1 Answer

Weird gameplay slow down problem 1 Answer

How to save and display players times using Playerprefs in Javascript? 1 Answer

A node in a childnode? 1 Answer

Move character left and right onTouch Unity C# 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