- Home /
Question by
laurienash · Apr 01, 2015 at 10:53 AM ·
javascriptstatic
Randomise pixel offset to create TV static effect
Hi!
I'd like to create a TV static effect - I've applied a texture to a plane and assigned it the particles additive shader.
I've attached this script (I want the pixel offset to be randomised each frame) - but at the moment it's just created a loop, with the pixels moving forward each frame to give a conveyor belt like effect.
Do you know how I can change this so it's randomised?
#pragma strict
var scrollSpeed : float = 0.5;
var rend: Renderer;
function Start() {
rend = GetComponent.<Renderer>();
}
function Update () {
var offset : float = Time.time * scrollSpeed;
rend.material.SetTextureOffset("_MainTex", Vector2(offset,0));
}
Best, Laurien
Comment
Best Answer
Answer by HarshadK · Apr 01, 2015 at 11:12 AM
You can use:
function Update () {
// This will generate a random value for offset between 0 and Time.time * scrollSpeed
var offset : float = Random.Range(0, Time.time * scrollSpeed);
rend.material.SetTextureOffset("_MainTex", Vector2(offset,0));
}
Instead of 0 you can even use a minValue. You might also want to play with the value of scrollSpeed.
Yay thanks so much :)
In case this is helpful to anyone else I switched $$anonymous$$athf.Random for Random.Range as the first threw up errors:
function Update () {
var offset : float = Random.Range(0, Time.time * scrollSpeed);
rend.material.SetTextureOffset("_$$anonymous$$ainTex", Vector2(offset,0));
}