Search code examples
unity-game-enginescrolltexturesoffset

unity - texture scroll speed with gameobject


I have a board in unity3D and I have a cube onto board. Board has got texture and texture offset changing by Y coordinate so it seems like moving backward. Cube should move also with same speed as board's offset but I couldn't set the same speed between them.

My board scroll code :

public class moveBoard : MonoBehaviour
{

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{

    this.GetComponent<MeshRenderer>().material.SetTextureOffset("_MainTex", new Vector2(0, -1 * Time.time));
}
}

And my cube move code :

public class moveTus : MonoBehaviour
{
public GameObject board;
float offsetY = 0f;
// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
    this.transform.Translate(Vector3.back * -10 * Time.deltaTime) ;
}
}

So I need to move the cube with same speed of the board's offset speed.


Solution

  • Include a public speed variable into both scripts.

    public class moveBoard : MonoBehaviour { public float speed=1; void Update () { this.GetComponent().material.SetTextureOffset("_MainTex", new Vector2(0, -1 * Time.deltaTime * speed * UserOptions.speed)); } }

    public class moveTus : MonoBehaviour { public float speed=1; void Update () { this.transform.Translate(Vector3.back * -10 * Time.deltaTime * speed * UserOptions.speed) ; } }

    During runtime try to sync by changing any of these speed variables values manually in Editor Inspector. After you find out a fine tune between them, apply these values in design time.