✧ scripts ✧


a place to dump my scripts so i have em all in one place...



✧ day/night cycle ✧
✧ follow the player ✧
✧ walk randomly ✧
✧ animated textures✧


✧ day/night cycle ✧


using UnityEngine;
using System.Collections;

[System.Serializable]
public class DayColors
{
public Color skyColor;
public Color equatorColor;
public Color horizonColor;
}

public class DayAndNightControl : MonoBehaviour {
public bool StartDay; //start game as day time
public GameObject StarDome;
public GameObject NightDome;
public GameObject moonState;
public GameObject moon;
public DayColors dawnColors;
public DayColors dayColors;
public DayColors nightColors;
public int currentDay = 0; //day 8287... still stuck in this grass prison... no esacape... no freedom...
public Light directionalLight; //the directional light in the scene we're going to work with
public float SecondsInAFullDay = 120f; //in realtime, this is about two minutes by default. (every 1 minute/60 seconds is day in game)
[Range(0,1)]
public float currentTime = 0; //at default when you press play, it will be nightTime. (0 = night, 1 = day)
[HideInInspector]
public float timeMultiplier = 1f; //how fast the day goes by regardless of the secondsInAFullDay var. lower values will make the days go by longer, while higher values make it go faster. This may be useful if you're siumulating seasons where daylight and night times are altered.
public bool showUI;
float lightIntensity; //static variable to see what the current light's insensity is in the inspector
Material starMat;
Material nightMat;

float timeLeft;
Color targetColor;

Camera targetCam;

// Use this for initialization
void Start () {
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Trilight;
foreach (Camera c in GameObject.FindObjectsOfType())
{
if (c.isActiveAndEnabled) {
targetCam = c;
}
}
lightIntensity = directionalLight.intensity; //what's the current intensity of the light
starMat = StarDome.GetComponentInChildren ().material;
if (StartDay) {
currentTime = 0.3f; //start at morning
starMat.color = new Color(1f,1f,1f,0f);
}
nightMat = NightDome.GetComponentInChildren().material;
if (StartDay)
{
currentTime = 0.3f; //start at morning
nightMat.color = new Color(1f, 1f, 1f, 0f);
}
}

// Update is called once per frame
void Update () {
UpdateLight();
currentTime += (Time.deltaTime / SecondsInAFullDay) * timeMultiplier;
if (currentTime >= 1) {
currentTime = 0;//once we hit "midnight"; any time after that sunrise will begin.
currentDay++; //make the day counter go up
}
}

void UpdateLight()
{
StarDome.transform.Rotate (new Vector3 (0, 2f * Time.deltaTime, 0));
moon.transform.LookAt (targetCam.transform);
directionalLight.transform.localRotation = Quaternion.Euler ((currentTime * 360f) - 90, 170, 0);
moonState.transform.localRotation = Quaternion.Euler ((currentTime * 360f) - 100, 170, 0);
//^^ we rotate the sun 360 degrees around the x axis, or one full rotation times the current time variable. we subtract 90 from this to make it go up
//in increments of 0.25.

//the 170 is where the sun will sit on the horizon line. if it were at 180, or completely flat, it would be hard to see. Tweak this value to what you find comfortable.

float intensityMultiplier = 1;

if (currentTime <= 0.23f || currentTime >= 0.75f)
{
intensityMultiplier = 0; //when the sun is below the horizon, or setting, the intensity needs to be 0 or else it'll look weird
starMat.color = new Color(1,1,1,Mathf.Lerp(1,0,Time.deltaTime));

}
else if (currentTime <= 0.25f)
{

intensityMultiplier = Mathf.Clamp01((currentTime - 0.23f) * (1 / 0.02f));
starMat.color = new Color(1,1,1,Mathf.Lerp(0,1,Time.deltaTime));

}
else if (currentTime <= 0.73f)
{

intensityMultiplier = Mathf.Clamp01(1 - ((currentTime - 0.73f) * (1 / 0.02f)));

}

//change env colors to add mood

if (currentTime <= 0.2f) {
RenderSettings.ambientSkyColor = Color.Lerp(RenderSettings.ambientSkyColor, nightColors.skyColor,Time.deltaTime);
RenderSettings.ambientEquatorColor = Color.Lerp(RenderSettings.ambientEquatorColor, nightColors.equatorColor, Time.deltaTime);
RenderSettings.ambientGroundColor = Color.Lerp(RenderSettings.ambientGroundColor, nightColors.horizonColor, Time.deltaTime);
}
if (currentTime > 0.2f && currentTime < 0.4f) {
RenderSettings.ambientSkyColor = Color.Lerp(RenderSettings.ambientSkyColor, dawnColors.skyColor, Time.deltaTime);
RenderSettings.ambientEquatorColor = Color.Lerp(RenderSettings.ambientEquatorColor, dawnColors.equatorColor, Time.deltaTime);
RenderSettings.ambientGroundColor = Color.Lerp(RenderSettings.ambientGroundColor, dawnColors.horizonColor, Time.deltaTime);

}
if (currentTime > 0.4f && currentTime < 0.75f) {
RenderSettings.ambientSkyColor = Color.Lerp(RenderSettings.ambientSkyColor, dayColors.skyColor, Time.deltaTime);
RenderSettings.ambientEquatorColor = Color.Lerp(RenderSettings.ambientEquatorColor, dayColors.equatorColor, Time.deltaTime);
RenderSettings.ambientGroundColor = Color.Lerp(RenderSettings.ambientGroundColor, dayColors.horizonColor, Time.deltaTime);

}
if (currentTime > 0.75f) {
RenderSettings.ambientSkyColor = Color.Lerp(RenderSettings.ambientSkyColor, dayColors.skyColor, Time.deltaTime);
RenderSettings.ambientEquatorColor = Color.Lerp(RenderSettings.ambientEquatorColor, dayColors.equatorColor, Time.deltaTime);
RenderSettings.ambientGroundColor = Color.Lerp(RenderSettings.ambientGroundColor, dayColors.horizonColor, Time.deltaTime);

}

directionalLight.intensity = lightIntensity * intensityMultiplier;
}

public string TimeOfDay ()
{
string dayState = "";
if (currentTime > 0f && currentTime < 0.1f) {
dayState = "Midnight";
if (timeLeft <= Time.deltaTime)
{
//nightMat.color = new Color(0.1580634f, 0.1817454f, 0.3018868f, 0.9f);

// transition complete
// assign the target color
nightMat.color = targetColor;

// start a new transition
targetColor = new Color(0.1580634f, 0.1817454f, 0.3018868f, 0.9f);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
nightMat.color = Color.Lerp(nightMat.color, targetColor, Time.deltaTime / timeLeft);

// update the timer
timeLeft -= Time.deltaTime;
}
}

if (currentTime < 0.5f && currentTime > 0.1f)
{
dayState = "Morning";
//nightMat.color = new Color(0.8490566f, 0.6047526f, 0.8155605f, 0.5f);
if (timeLeft <= Time.deltaTime)
{

// transition complete
// assign the target color
nightMat.color = targetColor;

// start a new transition
targetColor = new Color(0.8490566f, 0.6047526f, 0.8155605f, 0.5f);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
nightMat.color = Color.Lerp(nightMat.color, targetColor, Time.deltaTime / timeLeft);

// update the timer
timeLeft -= Time.deltaTime;
}
}
if (currentTime > 0.5f && currentTime < 0.6f)
{
dayState = "Mid Noon";
//nightMat.color = new Color(0.2755005f, 0.2367391f, 0.3773585f, 0f);
if (timeLeft <= Time.deltaTime)
{

// transition complete
// assign the target color
nightMat.color = targetColor;

// start a new transition
targetColor = new Color(0.2755005f, 0.2367391f, 0.3773585f, 0f);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
nightMat.color = Color.Lerp(nightMat.color, targetColor, Time.deltaTime / timeLeft);

// update the timer
timeLeft -= Time.deltaTime;
}
}
if (currentTime > 0.6f && currentTime < 0.8f)
{
dayState = "Evening";
//nightMat.color = new Color(0.6218847f, 0.3571111f, 0.7075472f, 0.5f);
if (timeLeft <= Time.deltaTime)
{

// transition complete
// assign the target color
nightMat.color = targetColor;

// start a new transition
targetColor = new Color(0.6218847f, 0.3571111f, 0.7075472f, 0.5f);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
nightMat.color = Color.Lerp(nightMat.color, targetColor, Time.deltaTime / timeLeft);

// update the timer
timeLeft -= Time.deltaTime;
}
}
if (currentTime > 0.8f && currentTime < 1f)
{
dayState = "Night";
// nightMat.color = new Color(0.2755005f, 0.2367391f, 0.3773585f, 0.8f);
if (timeLeft <= Time.deltaTime)
{

// transition complete
// assign the target color
nightMat.color = targetColor;

// start a new transition
targetColor = new Color(0.2755005f, 0.2367391f, 0.3773585f, 0.8f);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
nightMat.color = Color.Lerp(nightMat.color, targetColor, Time.deltaTime / timeLeft);

// update the timer
timeLeft -= Time.deltaTime;
}
}
return dayState;
}

void OnGUI()
{
//debug GUI on screen visuals
if (showUI) {
GUILayout.Box ("Day: " + currentDay);
GUILayout.Box (TimeOfDay ());
GUILayout.Box ("Time slider");
GUILayout.VerticalSlider (currentTime, 0f, 1f);
}
}
}




✧ follow the player ✧


SC_NPCFollow.cs

using UnityEngine;
using UnityEngine.AI;

public class SC_NPCFollow : MonoBehaviour
{
//Transform that NPC has to follow
public Transform transformToFollow;
//NavMesh Agent variable
NavMeshAgent agent;

// Start is called before the first frame update
void Start()
{
agent = GetComponent();
}

// Update is called once per frame
void Update()
{
//Follow the player
agent.destination = transformToFollow.position;
}
}


✧ walk randomly ✧

(character agent = npc, destination change = empty game object)

CharacterAgent.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class CharacterAgent : MonoBehaviour
{
public GameObject characterDesination;
NavMeshAgent theAgent;

void Start()
{
theAgent = GetComponent();
}

void Update()
{
theAgent.SetDestination(characterDesination.transform.position);
}
}

DestinationChange.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestinationChange : MonoBehaviour
{
public int xPos;
public int zPos;

void OnTriggerEnter(Collider other)
{
if (other.tag == "Character")
{
xPos = Random.Range(312, 559);
zPos = Random.Range(-270, -70);
this.gameObject.transform.position = new Vector3(xPos, 27f, zPos);
}
}

}

✧ animated textures ✧

(uses sprite sheets)

AnimatedTexture.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//AnimatedTexture
public class AnimatedTexture : MonoBehaviour
{

//vars for the whole sheet
public int colCount = 4;
public int rowCount = 4;

//vars for animation
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 4;
public int fps = 10;

//private vars
private Vector2 offset;
private Renderer renderer;

//Start
void Start() { renderer = this.GetComponent(); }

//Update
void Update() { SetSpriteAnimation(colCount, rowCount, rowNumber, colNumber, totalCells, fps); }

//SetSpriteAnimation
void SetSpriteAnimation(int colCount, int rowCount, int rowNumber, int colNumber, int totalCells, int fps)
{

// Calculate index
int index = (int)(Time.time * fps);
// Repeat when exhausting all cells
index = index % totalCells;

// Size of every cell
float sizeX = 1.0f / colCount;
float sizeY = 1.0f / rowCount;
Vector2 size = new Vector2(sizeX, sizeY);

// split into horizontal and vertical index
var uIndex = index % colCount;
var vIndex = index / colCount;

// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
float offsetX = (uIndex + colNumber) * size.x;
float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
Vector2 offset = new Vector2(offsetX, offsetY);

renderer.material.SetTextureOffset("_MainTex", offset);
renderer.material.SetTextureScale("_MainTex", size);
}
}