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 SomeGuy22 · Apr 02, 2013 at 03:59 PM · yieldtranslateeffectslowinconsistent

Translate and Yield giving inaccurate results?

I need to transform.Translate (x, 0, 0) and yield (x seconds), and that seems to work fine and all, but I also need to incorporate a "fake-slow motion" effect (without using Time.timeScale) and my solution was to

transform.Translate (x * y, 0, 0) and yield (x / y seconds) where y is a fraction.

However produces different results from the original x translation and yield because the object ends up in a different place then where it should be. If I'm correct, multiplying and dividing the translate and yield should end up in the same place over a longer amount of time... right?

Thanks in advance!

Comment
Add comment · Show 8
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 TASNO · Apr 02, 2013 at 04:00 PM 0
Share

I just want to ask what game are you making mate :D

avatar image SomeGuy22 · Apr 02, 2013 at 11:53 PM 0
Share

A 2D puzzle platformer inspired by Inception with multiple "levels" each corresponding to different parts of the brain. And as you go farther down, just like in Inception, the higher levels become more and more slow, meaning you have to time things just right to make changes in the dreams within dreams.

Thanks for asking :D

avatar image robertbu · Apr 03, 2013 at 12:13 AM 1
Share

Your logic does not make sense to me. If y is a fraction, it mean each step will be shorter, and each yield will be longer. I assume you do this in a loop. What is your end condition for the loop? Why not just increase the yield and leave the Translate() alone?

avatar image Benproductions1 · Apr 03, 2013 at 12:47 AM 1
Share

Why do this with yield, when you can just change your movement speed? Don't you use Time.deltaTime?

avatar image SomeGuy22 · Apr 03, 2013 at 01:21 AM 0
Share

$$anonymous$$aybe this will help:

 function Update () {
     
     if (move)
     {
         if (level == 1)
         {
             newSpeed = (-speed * mc.GetComponent(LevelController).value1);
             waitTime = time / mc.GetComponent(LevelController).value1;
         transform.Translate( newSpeed * Time.deltaTime, 0, 0);
         
         }
     }
 }
 
 function Activate () {
     
     move = true;
     if (level == 1)
     yield Wait (time);
     // / mc.GetComponent(LevelController).value1);
     move = false;
     
 }
 
 function Wait (ntime : float) {
     waitTime = ntime;
     var t = 0.0;
     while (t < waitTime) {
     t += Time.deltaTime;
     yield;
     }
 }
 
 
Show more comments

2 Replies

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

Answer by SomeGuy22 · Apr 06, 2013 at 12:37 AM

Okay so instead I just used a trigger system that turns off the movement instead of trying to time it, so now it only has to reduce the speed and no matter what speed when it hits the trigger it'll stop at that point.

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 Bunny83 · Apr 03, 2013 at 02:05 AM

I'm wondering why you use a yield in the first place... To get a smooth movement you should update your position every visual frame (so each update). When using WaitForSeconds the movement just gets choppy.

If I can't use Time.timeScale i would create a deltaTime wrapper class. Every object that has a movement function needs to use a wrapper instead of Time.deltaTime.

Something like that:

 // C#
 public class DTProvider
 {
     private static Stack<float> m_Layers = new Stack<float>();
     private static float m_CurrentRealTime = 1.0f;
 
     public static void AddLayer(float aScale)
     {
         m_Layers.Push(m_CurrentRealTime);
         m_CurrentRealTime *= aScale;
     }
     public static void RemoveLayer()
     {
         if (m_Layers.Count > 0)
             m_CurrentRealTime = m_Layers.Pop();
     }
 
     private float m_Scale = 1.0f;
 
     public DTProvider()
     {
         m_Scale = 1.0f / m_CurrentRealTime;
     }
     public DTProvider(float aScale)
     {
         m_Scale = aScale;
     }
     
     public float DT
     {
         get
         {
             return Time.deltaTime * m_CurrentRealTime * m_Scale;
         }
     }
 
     public static implicit operator float(DTProvider aProvider)
     {
         return aProvider.DT;
     }
 }

Every script that controls movement should create such a wrapper in Awake. That way it's movement is bound to the current time layer. All movement is simply multiplied with the DT value of the provider. When a new layer is added it will become the new "realtime" and everything from the "old" layer will get slower. When you remove a layer, objects on a deeper layer will get faster.

A movement script could look like this:

 public class Movement : UnitySingleton<App>
 {
     DTProvider DT;
     void Awake()
     {
         DT = new DTProvider();
     }
     void Update()
     {
         transform.Translate(5.0f*DT, 0, 0);
     }
 }

You can also create a provider for a certain timescale:

     DT = new DTProvider(4.0f);

this would make the object move 4 times faster than realtime. Now when you add two layers of 0.5f the object would move at normal speed since 0.5f 0.5f == 0.25f. Since the object's private timescale is 4 the result will be 1.0f (==0.25f 4.0f). If you add another layer of 0.5f the speed will continue to slow down.

Comment
Add comment · Show 1 · 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 SomeGuy22 · Apr 03, 2013 at 04:00 PM 0
Share

Thank you I understand what you're saying, but I'm talking about turning the movement on and off which means yielding to change the "move" variable off when an external script turns it on with a function. I don't see how I could use any other method to turn off movement after a set amount of time.... unless I used Triggers which could be a last resort, but I don't see why I can't wait for a scaled amount of time and then turn off the movement variable.

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

12 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

Related Questions

How to translate code with yield waitforseconds to c#? 1 Answer

Extreme amounts of processing crash android build? 1 Answer

Object keeps moving up even when StopCoroutine is called 2 Answers

Problem with scripts 1 Answer

Placing objects on a grid 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