scrolling a background

I’m scrolling a cloud texture in a 2D game to give the appearance of clouds moving across the sky. I have it working but wondered if there were some tricks I was missing.

Essentially, I’m usng GUI.DrawTexture to draw the texture at a continually changing x coordinate and repeating the texture on the right. I thought there could be a better way, like a “wrapping” setting I could use. My code example is below.

Thanks,

Brian

var clouds : Texture2D;
var cloudPos : float = 0;

function OnGUI ()
{
	...

	GUI.DrawTexture(Rect(-cloudPos, 0, 480, 160), clouds, ScaleMode.ScaleAndCrop);
	GUI.DrawTexture(Rect(480 - cloudPos, 0, 480, 160), clouds, ScaleMode.ScaleAndCrop);

	if (cloudPos < 480)
		cloudPos += Time.deltaTime * 10;
	else
		cloudPos = 0;
}

[/code]

Have a separate camera draw your skybox. Rotate that camera. Moving clouds with minimal effort. If you wish you can also disconnect the skybox from the clouds, by having a camera inside a hemisphere with clouds drawn on it and rotate it, instead of your skybox.

I’m sure that will work, but this is a 2D game so I don’t think I need a sky box at all. At most, I need a “sky plane”. Knowing this, is there a better technique than the one I’m using?

Thanks,

Brian

Use a 3D plane with the texture on it, and scroll the UV offset. (Look up SetTextureOffset in the docs for an example.)

–Eric

Similar to what you’re doing, but different from using a GUItexture.

Create a plane, put your sky/cloud texture on it and scale it to fit.

function Update() {

var scrollSpeed = 0.5;
var offset = Time.time * scrollSpeed;

 renderer.material.mainTextureOffset = Vector2 (offset, 0);

}

This will constantly scroll and ‘wrap’ your texture to the right assuming it’s tileable. Myself i’d still use a skybox, otherwise you have to use a plane and texture oversized in the x direction, or it will get quite repetitive.

Thanks for the help. And just to show how green I am, can you clarify what is meant by the “main texture”? Is this the texture for a material whose shader uses additional textures? For shaders like diffuse, there is only one texture available, right?

– Brian

main texture is the texture that is on the plane, the first one you put on it.

Sorry, but I don’t understand this. Is there a way to put 2 textures on a plane? If I drag a 2nd texture to a plane it replaces the first one and uses the default “diffuse” shader. So if this is the “main texture”, what is an example of 2nd texture? Is this for a shader such as “decal” that uses more than one texture?

photoshop

combine the textures to put two on a plane

This doesn’t address my question, which asked about the significance of the “main texture” and how to add a 2nd texture to a plane so that it will be recognized a texture #2 by Unity. I’m not talking about graphical stuff here, which of course I know you can do in Photoshop. I’m talking about learning Unity data structures and programming.

– Brian

oh

Correct. Main texture references the diffuse map. You can however easily access the normal, specular and whatnot for shaders that use them with the following…

 renderer.material.GetTexture ("_BumpMap");

In fact, renderer.maintexture is just a shorthand equivalent for…

 renderer.material.GetTexture ("_MainTex");

Try looking around in the documentation for the Renderer Class to see all the goodies you have access to.