A character that changes background behind him.

Ooookay. So i got an idea, witch i find hard to explain so i’ll try to demonstrate it with a pictures.

So there a circle witch surrounds the players. The background is black and white, (with some trees) but as you can see the player is some kind of dark creature… So the background around him is another picture of the same background. But looks a little darker, the trees is a little different. And the same with the lighter character.

So what i’m trying to explain(with my baaad english) is: Can you show a different background behind the player without changing the whole background?

Can you lead me to the right direction?

Thanks for the help :slight_smile:

-August

I guess your idea is 2D. But you can use the same concept in 3D. See below

◆Create a Plane , put it behind your character , scale it to fit your character. That is your background range.

◆Create a script (js recommand) , and attach to the Plane

Here is the script

gameObject.renderer.material.SetColor("_Color",Color.red);

It may change the plane’s color to red, you can put this line in OnMouseDown().

And here is the color code : http://docs.unity3d.com/Documentation/ScriptReference/Color.html

you just need to switch Color.red to Color.green or Color.blue

Or, if you need the custom color you can do this

gameObject.renderer.material.SetColor("_Color",Color(R,G,B,A));

As with most things in Unity, there are multiple solutions. I Assuming your character always walks in front of the trees. Below is a recipe for one solution:

  • Construct four backgrounds textures. The no-character background, and a background for each character.
  • Construct a material for the no-character (base) background. Use Unlit/Texture with the texture you created .
  • Construct three other materials, one each for the three character backgrounds. Use the Procedural Circle Mask shader found in the Unity Wiki with a character texture for each.
  • Construct four Quads and size them to exactly fit the game window. There are solutions that don’t require the Quads to fit, but I wanted you to get things up and running first.
  • Each Quad gets one of the four materials you created in the earlier steps.
  • Order the Quads by giving them different ‘z’ values. The no-character (base) Quad should be in the back.
  • Disabling all but one Quad, in turn adjust the Outer Radius, Inner Radius, and Hardness parameters to get a circle the right size and right edge softness.
  • Add the following script to each of the Quads.
  • Add three characters to the scene.
  • In turn select each of the character quads and drag and drop one character on the ‘character’ variable on the script.
  • Hit play and move character around in the Inspector.

Note that the following script is using ScreenToViewportPoint() to calculate where to place the circle. This will only work if the Quads are sized to exactly fit the game window. If your textures are smaller or larger than the game window, you can do a Collider.Raycast() of the character against his Quad and use the RaycastHit.textureCoord instead. The script:

#pragma strict

public var character : Transform;

private var z = 1.0;
private var w : float;

function Start () {
	w = 1.0 * Screen.height / Screen.width;
}

function LateUpdate () {
	var pos = Camera.main.WorldToViewportPoint(character.position);
	var v = Vector4(pos.x, pos.y, z, w);
	 renderer.material.SetVector("_Distort", v);

}