- Home /
Terraria lighting
sorry for my bad english . i saw a lot of posts and i can't solve this problem . i making a 2D sandbox game like Terraria and Starbound . this is my game :
i can make a basic lighting but the lighting if filling the whole terrain . i want this :
someone can help me please ?
Answer by AnotherValidUsername · Jan 19, 2017 at 09:08 PM
Every Tile needs to have access to any of the surroundet tiles. They should have the same Script. My Full Script for this is not ready, but here are some Pieces:
public GameObject LeftBlock;
public GameObject RightBlock;
public GameObject UpBlock;
public GameObject DownBlock;
public string Improve;
public int Blockvalue; //Just the identification Number, Blockvalue 0 = air
public int Lightlevel = 5; //The Lightlevel of this Block, 15 = max.
public bool Lighted = false; //Whether a Lightsource is near the Block
private Color LightTone;
private bool Surroundet = false; //Whether the Block is totally surroundet by other Tiles
public bool free = false;
public bool Walled = false; //Whether the Block touches a background wall
public bool Surroundable = true;
private float FreeFloat;
public bool CHANGE = false;int LightLevelSeek(int Minusint)
{
int ReturnLevel = 0;
int Right = 0;
int Left = 0;
int Up = 0;
int Down = 0;
if (RightBlock != null) { Right = RightBlock.GetComponent<Block>().Lightlevel; }
if (LeftBlock != null) { Left = LeftBlock.GetComponent<Block>().Lightlevel; }
if (UpBlock != null) { Up = UpBlock.GetComponent<Block>().Lightlevel; }
if (DownBlock != null) { Down = DownBlock.GetComponent<Block>().Lightlevel; }
ReturnLevel = System.Math.Max(System.Math.Max(System.Math.Max(Left, Right), Up), Down);
return ReturnLevel - Minusint;
}
void Rendering()
{
if (CHANGE)
{
if (RightBlock != null && LeftBlock != null && UpBlock != null && DownBlock != null && Surroundable)
{ Surroundet = true; free = false; }
if (RightBlock == null || LeftBlock == null || UpBlock == null || DownBlock == null)
{ Surroundet = false; free = false; }
if (RightBlock == null && LeftBlock == null && UpBlock == null && DownBlock == null)
{ Surroundet = false; free = true; }
CHANGE = false;
}
if (Lighted && Blockvalue != 0)
{ Lightlevel = 15; }
else if (!Lighted)
{
if (Walled || Surroundet)
{
if (LightLevelSeek(3) > 0)
{ Lightlevel = LightLevelSeek(3); }
else
{ Lightlevel = 1; }
}
if (!Walled && !Surroundet)
{
Lightlevel = 15;
}
}
if (Blockvalue == 0 && Walled)
{
FreeFloat = (float)Lightlevel / 15;
LightTone.a = 1F - FreeFloat;
GetComponent<SpriteRenderer>().color = LightTone;
}
else if (Blockvalue == 0 && !Walled)
{
GetComponent<SpriteRenderer>().color = Color.clear;
}
else
{
LightTone.r = (float)Lightlevel / 15;
LightTone.g = (float)Lightlevel / 15;
LightTone.b = (float)Lightlevel / 15;
GetComponent<SpriteRenderer>().color = LightTone;
}
}
Hope, that will work, just Update Rendering when The Tile is On the Screen, else it would take too much performance down...
Okay, your post is almost three years old... I have a question. You say, add a script to every tile? Serious? First. You cannot add a script to a tile in unitys tilemap system.
So then you mean, write your own tilemap system and every tile is actually a gameobject?? So we talking about terraria where the smallest world has 4 million tiles. You want create 4 million gameobjects? $$anonymous$$aybe i am wrong but i don't think this could work somehow...
Is there any solution how to make that terraria lightning for tilemaps? :-)