- Home /
Change multiple gameobjects colour to create a trail
Hi
I have a grid type game where the player moves a cube 1 unit at a time in a grid like fashion (think like a chess board). On the player cube, there is a collider and on the board pieces there is a collider too. What I am trying to achieve is that each time the player moves to a new board piece, that particular piece gets brighter (modify the colour settings on that gameobjects material). This part is easy enough but what I would like is that the next piece that the player moves to gets its colour modified the brightest, and the previous one (that was the brightest), moves down in intensity. This continues for 4 pieces on the board so it shows the previous 4 moves of the player and they appear to fade colour in order of what was hit last.
I have an empty gameobject caller Trail which holds the script. This is where I think all this should be managed:
using UnityEngine;
using System.Collections;
public class Trail : MonoBehaviour {
public GameObject block1;
public GameObject block2;
public GameObject block3;
public GameObject block4;
// Use this for initialization
void Start () {
block1 = GameObject.Find("StartBlock").transform.FindChild("CubeWalls").gameObject;//the child object with the renderer on it
}
public void Shuffle() //move the blocks down one position
{
block2 = block1;
block3 = block2;
block4 = block3;
ChangeColour();
}
void ChangeColour() //lighten the colour on each block by x amount
{
Color color = block1.renderer.materials[0].color;
color.r += 0.2f;
color.g += 0.2f;
color.b += 0.2f;
block1.renderer.material.color = color;
Color color2 = block2.renderer.materials[0].color;
color2.r += 0.4f;
color2.g += 0.4f;
color2.b += 0.4f;
block2.renderer.material.color = color2;
Color color3 = block3.renderer.materials[0].color;
color3.r += 0.6f;
color3.g += 0.6f;
color3.b += 0.6f;
block3.renderer.material.color = color3;
Color color4 = block4.renderer.materials[0].color;
color4.r += 0.8f;
color4.g += 0.8f;
color4.b += 0.8f;
block4.renderer.material.color = color4;
}
}
Then on the pieces with the colliders, they should assign the current piece to the Trail script and then call shuffle on that script which should move the blocks down a position and subsequently change their colours but its within this logic that the thing doesn't work.
using UnityEngine;
using System.Collections;
public class CheckColourChange : MonoBehaviour {
public GameObject trail;
public Trail traily;
void Start()
{
trail = GameObject.Find("Trail"); //get the Trail gameobject that holds the script
traily = trail.GetComponent<Trail>(); //get the Trail script from the Trail gameobject
}
void OnTriggerEnter ( Collider other )
{
traily.Shuffle(); //move all the blocks in the Trail script down a position
traily.block1 = this.gameObject; //assign this block to be block 1 in the Trail script
}
}
It seems to assign the previous piece to block2, block3 and block4 in the Trail script rather than moving them all one by one. So I guess there is a problem in the Shuffle function. I am also aware that I have my color values around the wrong way but thats an easy fix.
Also, it may be better to have the gameobjects in an array?
Any help appreciated.
Your answer
Follow this Question
Related Questions
Trail Renderer's colors don't apply to material 1 Answer
Procedural construction 1 Answer
Logic error help: arrays and nested for loops 1 Answer
Trail of color behind the player 2D c# 1 Answer
Copy pixel color values to array 1 Answer