- Home /
texture offset question
sorry for another beginner question. I'm testing this texture offset code on a plane:
var scrollSpeed:float; //set to .3
function Update () {
if(Input.GetKey("d")){
renderer.material.SetTextureOffset("_MainTex", new Vector2(Time.time*scrollSpeed, 0));
}
}
the scrolling works fine, but everytime I let go of the d key and press it again, i get a jump, like a quick offset.. I'm thinking it probably has something to do with the time code.. any ideas?
Thanks
Answer by Eric5h5 · Nov 03, 2010 at 12:07 AM
Instead of using Time.time, use your own timer variable, and have it increase when the key is down. The jump is because Time.time continues increasing whether you're holding the key or not.
Answer by Adam Rademacher · Nov 03, 2010 at 12:08 AM
Time.time moves regardless of whether the D key is down or not...it's just the time that has passed. So it will 'catch up' if you let go of the key and press it again.
I think what you want to do is something like this:
var scrollspeed : float = 0.3; private var offset : float = 0.0;
function Update() { if(Input.GetKey("d")) { offset += Time.deltaTime; renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0)); }
}
This code does essentially the same thing except that the offset will only add time if you have the key down.
Thanks everyone. Well, that makes sense lol, I used Time.time because I was looking at the textureoffset docs and they used it there, but I implemented it the wrong way.. Your code works perfectly, thanks!
Your answer
Follow this Question
Related Questions
Animation Tiling/Offset 0 Answers
What is wrong with my shader? 1 Answer
To Make Animated Textures, how do I create a Tiled Image? 1 Answer
3D Mesh Deformer in Unity3D 2 Answers
Animate Toon Water on Plane? 0 Answers