- Home /
SetTextureOffset, small offset skip?
I'm using a small SetTextureOffset script to make my background pan and repeat itself. It works perfectly, but when I call the script it skips a little offset. This is the script I'm using:
var actualSpeed : float;
private var scrollSpeed : float;
private var script : PlayerScript;
function Start () {
scrollSpeed = 0;
script = GameObject.FindObjectOfType(PlayerScript);
}
function Update () {
if(script.Fired){
scrollSpeed = actualSpeed;
var offset = Time.time * scrollSpeed;
renderer.material.SetTextureOffset ("_MainTex", Vector2(offset,0));
}
if(script.rigidbody.IsSleeping() && script.Fired){
scrollSpeed = 0;
}
}
Anyone any suggestions?
Not exactly, I rewrote some of the script to be more efficient. InvokeRepeating ins$$anonymous$$d of update, and just += some of the offset in that function. Though it works I still have the offset skip once I invoke the function. I'll get back to you when I know what causes it.
Answer by Eck · Nov 17, 2014 at 05:23 PM
Sorry for the Necromancy and I'm sure you've already solved this, but I'm tracking down a jitter in my scrolling background and found this. So maybe it will help someone else.
Your problem is you're using Time.time which is how much time has elapsed since the start of the program. So if you call Start() 10 seconds in to your program running, it jumps from 0 offset to 10 seconds worth of offset. If you stop the animation for 3 seconds, and start it back up, it will skip 3 seconds worth.
Instead, you can use Time.deltaTime and keep up with the offset between this frame and last frame. You'll also have to keep up with your total offset and use that.
private float offset = 0;
function Update()
{
if(script.Fired)
{
scrollSpeed = actualSpeed;
offset += Time.deltaTime * scrollspeed;
renderer.material.SetTextureOffset ("_MainTex", Vector2(offset,0));
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613081608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material offset relative to other game object 0 Answers
Scroll Ball Texture Smoothly 0 Answers
Texture offset not behaving like it should (?) 1 Answer
Material offset per object 0 Answers