- Home /
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;
}
}
}
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;
}
}
Your answer
Follow this Question
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