• 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
Question by costin88boss · Apr 29, 2020 at 12:01 PM · timespeedsmoothtimer countdownvectors

Time Freezer

so i want to make a time freezer in a script w$$anonymous$$ch enemies has it to move up and down, and i want the script to smoothly (smoothly really) move slower and slower and then stop, same as unfreezing time.

 private Vector3 pos1;
 private Vector3 pos2;
 public Vector3 posDiff = new Vector3(0f, 0f, 20f);
 public float speed = 1.0f;
 public bool Freezeable = true;
 public GameObject PistonObject;

 void Start()
 {

     pos1 = transform.position;
     pos2 = transform.position + posDiff;
 }
 bool Freeze = false;

 void Update()
 {



         if (Freeze == false)
         {
             transform.position = Vector3.Lerp(pos1, pos2, Mathf.PingPong(Time.time * speed, 1.0f));
         }

     if (Freeze == false && Input.GetKeyDown(KeyCode.E) && Freezeable == true)
     {
         speed -= Time.deltaTime;
         Freeze = true;
     }
     else if (Freeze == true && Input.GetKeyDown(KeyCode.E) && Freezeable == true)
     {
         speed += Time.deltaTime;
         Freeze = false;
     }

that is what i've done so far.

the problem is, -= and += doesn't smoothly change value.

the freezeable bool is just to make some enemies don't be affected (immune enemies.)

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Chris_Payne_QS · Apr 29, 2020 at 02:08 PM

On the frame that you press E it will subtract Time.deltaTime (a barely noticeable change) and set Freeze to true so your object stops moving instantly. You need to wait until speed reaches zero before you stop updating the position.

I would store a speedTarget variable and constantly lerp speed towards that value - then you only have to set speedTarget to 0 or 1 and the transition will happen automatically (even if interrupting an existing transition).

Also remove the Freeze flag and use if (speed>0f) to determine whether to update position.

Example:

 public float speed = 1f;
 private float speedTarget = 1f;
 
 void Update()
 {
     speed = Mathf.MoveTowards( speed, speedTarget, Time.deltaTime );
     if (speed>0f)
     {
         // do movement
     }

     if (Freezable)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             speedTarget = 1f - speedTarget; // 0 becomes 1, 1 becomes 0
         }
     }
 }
Comment

People who like this

0 Show 2 · 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 costin88boss · Apr 29, 2020 at 02:28 PM 0
Share

i did not see my Freeze mistake, but for the other stuff you said i don't really understand. maybe a link to the unity manual?

avatar image Chris_Payne_QS costin88boss · Apr 29, 2020 at 03:26 PM 0
Share

I added a code fragment to the answer, hope that helps...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

128 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 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 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 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 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

How do I make a Countdown Timer in Minutes and Seconds only 1 Answer

vehicle time traveled is incorrect according to speed (Unity time VS real time) 0 Answers

set the speed in respect of time 2 Answers

Play whole animation over specified time via script 1 Answer

interpolate a move variable 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