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 /
avatar image
0
Question by Gotal · Nov 07, 2016 at 04:00 PM · timevalueover

Need help with value over time!

Hey friendly people.

I have an object going forward by some float speed.

I'm trying to bring it down on trigger over time. My code looks something like this:

 public class ObjectStoppre: MonoBehaviour
 {
     public myObject object;  // not my real objects name
     float myTime;                    //just an example name
 
     void Update()
     {
         myTime = Time.deltaTime * 10;
     }
 
     public void OnTriggerEnter(Collider col)
     {
         if (col.tag == "frontOfObject")
         {
             Debug.Log("Stop me over time Senpai!");
             object.speedObject = Mathf.Lerp(object.speedObject, 0,myTime);
         }
     }
 }

So what i'm doing is grabing the speed value from my "Move" script and trying to lerp it over time to 0. I'm having no luck.. can someone help me out please?

Thanks

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 TreyH · Nov 07, 2016 at 04:10 PM 1
Share

It might help to know what that function is doing.

Your "myTime" value can be just about anything as you're not clamping it to [0,1], but that range is a requirement to get reasonable behavior from Lerp (Linear-Interpolation).

Additionally, you're calling this during an OnTriggerEnter event, which is usually not called multiple times in a row.

$$anonymous$$aybe you wanted to use OnTriggerStay ins$$anonymous$$d? $$anonymous$$aybe you want to start a coroutine to do that over time whenever something enters?

avatar image Gotal TreyH · Nov 09, 2016 at 09:22 AM 0
Share

Yea, i was using OnTriggerEnter wrong, i'll try with OnTriggerStay.. i'm trying to avoid using Update() because i have no idea how much of an impact it has on my performance.. and i need to optimize this "game" as much as i can (200fps $$anonymous$$imum).

Thanks for the reply, cheers

1 Reply

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

Answer by ThePersister · Nov 07, 2016 at 04:25 PM

Hi there friendly @Gotal.

Based on your code. Perhaps this helps? If it does, please accept my answer! :)

Otherwise, elaborate and I could try again.

 using UnityEngine;
 using System.Collections;
 
 public class ObjectStopper : MonoBehaviour
 {
     public myObject targetObject;  // not my real objects name
     public float stopSpeed = 10f;
     private bool isStopping;
 
     void Update()
     {
         if ( isStopping )
         {
             // Mathf.Max => Prevents negative speed.
             targetObject.speedObject = Mathf.Max(0f, targetObject.speedObject - stopSpeed * Time.deltaTime);
         }
     }
 
     void OnTriggerEnter( Collider col )
     {
         if( col.tag == "frontOfObject" )
         {
             Debug.Log( "Stop me over time Senpai!" );
             isStopping = true;
         }
     }
 }
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 ThePersister · Nov 07, 2016 at 04:26 PM 1
Share

You could add this to stop Stopping upon escape.

 void OnTriggerExit( Collider col )
      {
          if( col.tag == "frontOfObject" )
          {
              Debug.Log( "The great escape!" );
              isStopping = false;
          }
      }
avatar image Gotal · Nov 09, 2016 at 09:03 AM 1
Share

Hey man, thanks for the reply.. yea.. this works, but i kind of wanted to avoid using Update(), guess it cant be helped.. i'll try doing the same thing it an OnTriggerStay or running the function while speed != 0

Cheers

avatar image ThePersister Gotal · Nov 09, 2016 at 05:54 PM 1
Share

Should've said so, of course you can! :) We can achieve this using Coroutines / IEnumerators.

     using UnityEngine;
     using System.Collections;
 
     public class ObjectStopper : $$anonymous$$onoBehaviour
     {
         public myObject targetObject;  // not my real objects name
         public float stopSpeed = 10f;
         private bool isStopping;
 
         void OnTriggerEnter(Collider col)
         {
             if (col.tag == "frontOfObject")
             {
                 Debug.Log("Stop me over time Senpai!");
                 StartCoroutine( StopOverTime() );
             }
         }
 
         // Optional
         void OnTriggerExit(Collider col)
         {
             if (col.tag == "frontOfObject")
             {
                 Debug.Log("The great escape!");
                 isStopping = false;
             }
         }
         //
 
         // Alternative for using Update()
         private IEnumerator StopOverTime()
         {
             if (!isStopping)
             {
                 isStopping = true;
                 
                 // Update-like call, stops when speed becomes 0 or when isStopping is set to false.
                 while (isStopping && targetObject.speedObject >= 0)
                 {
                     // $$anonymous$$athf.$$anonymous$$ax => Prevents negative speed.
                     targetObject.speedObject = $$anonymous$$athf.$$anonymous$$ax(0f, targetObject.speedObject - stopSpeed * Time.deltaTime);
                     yield return new WaitForSeconds(0.01f);
                 }
             }
         }


I hope that leaves you a bit more satisfied! ;)

avatar image Gotal ThePersister · Nov 10, 2016 at 11:00 AM 1
Share

Ah man.. this will work wonders.. i need to read up a bit on IEnumerators and Coroutines.. the problem i had was, it stopped correctly in my editor, but my build gave me a bigger frame-rate, so my object stopped almost instantly.. thanks again man, cheers!

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

59 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

Related Questions

How would I decrease a variable over time based on distance? 1 Answer

Make value change to anoter over time and back loop 3 Answers

script not working :( 2 Answers

Increase a variable from a value to another value in a range of time 0 Answers

how to change a value over time in a coroutine ? 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