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 wuchiang · May 29, 2015 at 08:53 AM · animationtextureissuescrollinginterpolation

How to properly ease out texture scrolling?

I have a scrolling street texture (I already got that part working) and that I use to make it appear as if a car is constantly moving forward on it. I also want to be able to trigger the street texture to gradually stop scrolling. I've already written code for this using a coroutine and lerping my speed variable from 1 to 0, but this makes the texture scroll in reverse for some reason.

Here is a sample project that demonstrates this issue.

and here is my TextureScroller class:

 using UnityEngine;
 using System.Collections;
 
 public class TextureScroller : MonoBehaviour
 {
     public Vector2 mAnimationRate = new Vector2(1.0f, 0.0f);
     public float mSpeed = 1.0f; // This is customizable in the editor, but won't be changed in script
     public bool mScrollOnAwake = true;
     
     private Renderer mRenderer;
     private bool mScrolling = false;
     private float mScrollingSpeed = 1.0f;
     
     void Awake()
     {
         mScrollingSpeed = mSpeed;
         mRenderer = GetComponent<Renderer>();
         mScrolling = mScrollOnAwake;
     }
     
     void Update()
     {
         if (!mScrolling)
             return;
         
         Vector2 offset = mAnimationRate * mScrollingSpeed * Time.time;
         mRenderer.material.SetTextureOffset("_MainTex", offset);
     }
         
     public IEnumerator ToggleScrolling(bool enabled, float durationBeforeToggling = 0.0f)
     {
         if (durationBeforeToggling == 0.0f)
         {
             mScrollingSpeed = enabled ? mSpeed : 0.0f;
             mScrolling = enabled;
         }
         
         else
         {
             float elapsedTime = 0.0f;
             float startSpeed = mSpeed;
             float targetSpeed = enabled ? mSpeed : 0.0f;
             
             while (elapsedTime < durationBeforeToggling)
             {
                 elapsedTime += Time.deltaTime;
                 
                 mScrollingSpeed = Mathf.Lerp(startSpeed, targetSpeed, elapsedTime / durationBeforeToggling);
                 Debug.Log("Scrolling speed: " + mScrollingSpeed);
                 
                 yield return null;
             }

             mScrolling = enabled;
         }
     }
 }
 


Comment
Add comment
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
0
Best Answer

Answer by wuchiang · Jun 01, 2015 at 07:25 AM

Alright, I managed to solve it, the problem was just mathematical error on my part. The texture would go back because multiplying animation rate by speed and then Time.time would basically give a smaller offset than the previous one, and that was what was making the texture go in reverse.

Now I multiply those values by Time.deltaTime and add that to the current offset, this means that the offset keeps growing, and I can make it grow slower by playing with the speed.

That is the effect needed to make it appear as if it was gradually stopping, the offset needs to keep increasing but in smaller values every frame.

Here is my updated code:

 using UnityEngine;
 using System.Collections;
 
 public class TextureScroller : MonoBehaviour
 {
     public Vector2 mAnimationRate = new Vector2(1.0f, 0.0f);
     public float mSpeed = 1.0f; // This is customizable in the editor, but won't be changed in script
     public bool mScrollOnAwake = true;
     
     private Renderer mRenderer;
     private bool mScrolling = false;
     private float mScrollingSpeed = 1.0f;
     
     void Awake()
     {
         mScrollingSpeed = mSpeed;
         mRenderer = GetComponent<Renderer>();
         mScrolling = mScrollOnAwake;
     }
     
     void Update()
     {
         if (!mScrolling)
             return;
 
         Vector2 currentOffset = mRenderer.material.GetTextureOffset("_MainTex");
         Vector2 newOffset = currentOffset + (mAnimationRate * mScrollingSpeed * Time.deltaTime);
         mRenderer.material.SetTextureOffset("_MainTex", newOffset);
     }
     
     public IEnumerator ToggleScrolling(bool enabled, float durationBeforeToggling = 0.0f)
     {
         if (durationBeforeToggling == 0.0f)
         {
             mScrollingSpeed = enabled ? mSpeed : 0.0f;
         }
         
         else
         {
             float elapsedTime = 0.0f;
             float startSpeed = mSpeed;
             float targetSpeed = enabled ? mSpeed : 0.0f;
             
             while (elapsedTime < durationBeforeToggling)
             {
                 elapsedTime += Time.deltaTime;
                 
                 mScrollingSpeed = Mathf.Lerp(startSpeed, targetSpeed, elapsedTime / durationBeforeToggling);
                 
                 yield return null;
             }
         }
 
         mScrolling = enabled;
     }
 }
 

 
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

19 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

Related Questions

Help with textures 0 Answers

Best Practices on 2D Animation? 1 Answer

How to change the color of a texture through an animation? 1 Answer

Interpolate Movement in Line With Animation? 0 Answers

Losing texture connections when creating a prefab 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