Sprite texture not repeating unity

787 views Asked by At

I want to repeat texture on Quad but it is not repeating
here is my code

    using UnityEngine;
using System.Collections;

public class Track : MonoBehaviour {
    public float speed;
    Vector2 Offset;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Offset = new Vector2 (0, Time.deltaTime * speed);

        GetComponent<Renderer> ().material.mainTextureOffset = Offset;

    }
}

please someone help

1

There are 1 answers

0
mrogal.ski On BEST ANSWER

You have logic problem when assigning value to Offset. Explanation below:

void Update () {
    // Assume that Offset's value is { 2, 3 }
    Offset = new Vector2 (0, Time.deltaTime * speed);
    // Assume Time.DeltaTime's value is 0.12f
    // Assume speed's value is 10
    // Now to figure out the problem ( 0.12f * 10f = 1.2f )
    // Offset's value is now 1.2f
    GetComponent<Renderer> ().material.mainTextureOffset = Offset;
}

As you can see now, you're only assigning the value which will be almost the same every time. The value can vary only a bit just because of Time.DeltaTime will yield delta time between frames which can be approximately 0.0016f.

So to make it clear, you should not rely on Time.DeltaTime because it's almost constant value. First try to initialize your Offset in Start or Awake method:

void Start() {
    Offset = GetComponent<Renderer>().material.mainTextureOffset;
}

Then inside of your Update method you can just increase X value :

void Update() {
    Offset = new Vector2(Offset[0] + (Time.DeltaTime * speed), Offset[1]); 
    GetComponent<Renderer>().material.mainTextureOffset = Offset;
}