Spawn Different GameObject depending on Scale.,Spawning different Gameobject depending on scale.
Could anyway explain how I would use a different set of Gameobject in spawn tile should the current/last tile be equal to or below a certain scale?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SunningdaleTowerStack : MonoBehaviour
{
public Text ScoreText;
public Color32[] TileColours;
public Material stackMat;
public GameObject endPanel;
public AudioClip[] clips;
private const float TileBoundSize = 3.5f;
private const float TileMovingSpeed = 5.0f;
private const float PlayerErrorMargin = 0.25f;
private const float TileSizeGain = 0.25f;
private const int AmountForGrowTileCombo = 3;
private GameObject[] TheTowerStack;
private Vector2 TileBounds = new Vector2(TileBoundSize, TileBoundSize);
private int TowerStackIndex;
private int ScoreCount = 0;
private int GrowTileCombo = 0;
private int lastColorIndex = 0;
private Color32 startColor;
private Color32 endColor;
private float TileTransition = 0.0f;
private float TileSpeed = 3.0f;
private float SecondaryTilePos;
private float colorTransition = 0;
private bool isMovingOnX = true;
private bool GameOver = false;
private Vector3 WantedTilePos;
private Vector3 LastTilePos;
private void Start()
{
// TheTowerStack = GameObject.FindGameObjectsWithTag("Tile"); // spawms random model?
TheTowerStack = new GameObject[transform.childCount];
startColor = TileColours[0];
endColor = TileColours[1];
lastColorIndex = 1;
for (int i = 0; i < transform.childCount; i++)
{
TheTowerStack[i] = transform.GetChild(i).gameObject;
ColorMesh(TheTowerStack[i].GetComponent<MeshFilter>().mesh);
}
TowerStackIndex = transform.childCount - 1;
}
private void CreateTileCutOff(Vector3 pos, Vector3 scale)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.localPosition = pos;
go.transform.localScale = scale;
go.AddComponent<Rigidbody>();
go.GetComponent<MeshRenderer>().material = stackMat;
ColorMesh(go.GetComponent<MeshFilter>().mesh);
}
private void Update()
{
if (GameOver)
return;
if (Input.GetMouseButtonDown(0))
{
if (PlaceTowerTile())
{
SpawnTowerTile();
ScoreCount++;
ScoreText.text = ScoreCount.ToString();
}
else
{
EndGame();
}
}
MoveTowerTile();
// Move the stack
transform.position = Vector3.Lerp(transform.position, WantedTilePos, TileMovingSpeed * Time.deltaTime);
}
private void MoveTowerTile()
{
TileTransition += Time.deltaTime * TileSpeed;
if (isMovingOnX)
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(Mathf.Sin(TileTransition) * TileBoundSize, ScoreCount, SecondaryTilePos);
else
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(SecondaryTilePos, ScoreCount, Mathf.Sin(TileTransition) * TileBoundSize);
}
private void SpawnTowerTile()
{
TileTransition = 1.0f;
LastTilePos = TheTowerStack[TowerStackIndex].transform.localPosition;
TowerStackIndex--;
if (TowerStackIndex < 0)
TowerStackIndex = transform.childCount - 1;
WantedTilePos = (Vector3.down) * ScoreCount;
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(0, ScoreCount, 0);
TheTowerStack[TowerStackIndex].transform.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
ColorMesh(TheTowerStack[TowerStackIndex].GetComponent<MeshFilter>().mesh);
}
private bool PlaceTowerTile()
{
Transform t = TheTowerStack[TowerStackIndex].transform;
if (isMovingOnX)
{
float deltaX = LastTilePos.x - t.position.x;
if (Mathf.Abs(deltaX) > PlayerErrorMargin)
{
// Put something in the clips array before un-commenting this line
// AudioSource.PlayClipAtPoint (clips [0], Camera.main.transform.position);
// CUT THE TILE
GrowTileCombo = 0;
TileBounds.x -= Mathf.Abs(deltaX);
if (TileBounds.x <= 0)
return false;
float middle = LastTilePos.x + t.localPosition.x / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
CreateTileCutOff
(
new Vector3((t.position.x > 0)
? t.position.x + (t.localScale.x / 2)
: t.position.x - (t.localScale.x / 2)
, t.position.y
, t.position.z),
new Vector3(Mathf.Abs(deltaX), 1, t.localScale.z)
);
t.localPosition = new Vector3(middle - (LastTilePos.x / 2), ScoreCount, LastTilePos.z);
}
else
{
// Put something in the clips array before un-commenting this line
// AudioSource.PlayClipAtPoint (clips [1], Camera.main.transform.position);
if (GrowTileCombo > AmountForGrowTileCombo)
{
TileBounds.x += TileSizeGain;
if (TileBounds.x > TileBoundSize)
TileBounds.x = TileBoundSize;
float middle = LastTilePos.x + t.localPosition.x / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
t.localPosition = new Vector3(middle - (LastTilePos.x / 2), ScoreCount, LastTilePos.z);
}
GrowTileCombo++;
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, LastTilePos.z);
}
}
else
{
float deltaZ = LastTilePos.z - t.position.z;
if (Mathf.Abs(deltaZ) > PlayerErrorMargin)
{
// Put something in the clips array
// AudioSource.PlayClipAtPoint (clips [0], Camera.main.transform.position);
// CUT THE TILE
GrowTileCombo = 0;
TileBounds.y -= Mathf.Abs(deltaZ);
if (TileBounds.y <= 0)
return false;
float middle = LastTilePos.z + t.localPosition.z / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
CreateTileCutOff
(
new Vector3(t.position.x
, t.position.y
, (t.position.z > 0)
? t.position.z + (t.localScale.z / 2)
: t.position.z - (t.localScale.z / 2)),
new Vector3(t.localScale.x, 1, Mathf.Abs(deltaZ))
);
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, middle - (LastTilePos.z / 2));
}
else
{
// Put something in the clips array
// AudioSource.PlayClipAtPoint (clips [1], Camera.main.transform.position);
if (GrowTileCombo > AmountForGrowTileCombo)
{
if (TileBounds.y > TileBoundSize)
TileBounds.y = TileBoundSize;
TileBounds.y += TileSizeGain;
float middle = LastTilePos.z + t.localPosition.z / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, middle - (LastTilePos.z / 2));
}
GrowTileCombo++;
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, LastTilePos.z);
}
}
SecondaryTilePos = (isMovingOnX)
? t.localPosition.x
: t.localPosition.z;
isMovingOnX = !isMovingOnX;
return true;
}
private void ColorMesh(Mesh mesh)
{
Vector3[] vertices = mesh.vertices;
Color32[] colors = new Color32[vertices.Length];
colorTransition += 0.1f;
if (colorTransition > 1)
{
colorTransition = 0.0f;
startColor = endColor;
int ci = lastColorIndex;
while (ci == lastColorIndex)
ci = Random.Range(0, TileColours.Length);
endColor = TileColours[ci];
}
Color c = Color.Lerp(startColor, endColor, colorTransition);
for (int i = 0; i < vertices.Length; i++)
colors[i] = c;
mesh.colors32 = colors;
}
private void EndGame()
{
if (PlayerPrefs.GetInt("score") < ScoreCount)
PlayerPrefs.SetInt("score", ScoreCount);
GameOver = true;
// endPanel.SetActive(true);
TheTowerStack[TowerStackIndex].AddComponent<Rigidbody>();
}
public void OnButtonClick(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
,Could anyway explain how I would use a different set of Gameobject in spawn tile should the current/last tile be equal to or below a certain scale?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SunningdaleTowerStack : MonoBehaviour
{
public Text ScoreText;
public Color32[] TileColours;
public Material stackMat;
public GameObject endPanel;
public AudioClip[] clips;
private const float TileBoundSize = 3.5f;
private const float TileMovingSpeed = 5.0f;
private const float PlayerErrorMargin = 0.25f;
private const float TileSizeGain = 0.25f;
private const int AmountForGrowTileCombo = 3;
private GameObject[] TheTowerStack;
private Vector2 TileBounds = new Vector2(TileBoundSize, TileBoundSize);
private int TowerStackIndex;
private int ScoreCount = 0;
private int GrowTileCombo = 0;
private int lastColorIndex = 0;
private Color32 startColor;
private Color32 endColor;
private float TileTransition = 0.0f;
private float TileSpeed = 3.0f;
private float SecondaryTilePos;
private float colorTransition = 0;
private bool isMovingOnX = true;
private bool GameOver = false;
private Vector3 WantedTilePos;
private Vector3 LastTilePos;
private void Start()
{
// TheTowerStack = GameObject.FindGameObjectsWithTag("Tile"); // spawms random model?
TheTowerStack = new GameObject[transform.childCount];
startColor = TileColours[0];
endColor = TileColours[1];
lastColorIndex = 1;
for (int i = 0; i < transform.childCount; i++)
{
TheTowerStack[i] = transform.GetChild(i).gameObject;
ColorMesh(TheTowerStack[i].GetComponent<MeshFilter>().mesh);
}
TowerStackIndex = transform.childCount - 1;
}
private void CreateTileCutOff(Vector3 pos, Vector3 scale)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.localPosition = pos;
go.transform.localScale = scale;
go.AddComponent<Rigidbody>();
go.GetComponent<MeshRenderer>().material = stackMat;
ColorMesh(go.GetComponent<MeshFilter>().mesh);
}
private void Update()
{
if (GameOver)
return;
if (Input.GetMouseButtonDown(0))
{
if (PlaceTowerTile())
{
SpawnTowerTile();
ScoreCount++;
ScoreText.text = ScoreCount.ToString();
}
else
{
EndGame();
}
}
MoveTowerTile();
// Move the stack
transform.position = Vector3.Lerp(transform.position, WantedTilePos, TileMovingSpeed * Time.deltaTime);
}
private void MoveTowerTile()
{
TileTransition += Time.deltaTime * TileSpeed;
if (isMovingOnX)
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(Mathf.Sin(TileTransition) * TileBoundSize, ScoreCount, SecondaryTilePos);
else
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(SecondaryTilePos, ScoreCount, Mathf.Sin(TileTransition) * TileBoundSize);
}
private void SpawnTowerTile()
{
TileTransition = 1.0f;
LastTilePos = TheTowerStack[TowerStackIndex].transform.localPosition;
TowerStackIndex--;
if (TowerStackIndex < 0)
TowerStackIndex = transform.childCount - 1;
WantedTilePos = (Vector3.down) * ScoreCount;
TheTowerStack[TowerStackIndex].transform.localPosition = new Vector3(0, ScoreCount, 0);
TheTowerStack[TowerStackIndex].transform.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
ColorMesh(TheTowerStack[TowerStackIndex].GetComponent<MeshFilter>().mesh);
}
private bool PlaceTowerTile()
{
Transform t = TheTowerStack[TowerStackIndex].transform;
if (isMovingOnX)
{
float deltaX = LastTilePos.x - t.position.x;
if (Mathf.Abs(deltaX) > PlayerErrorMargin)
{
// Put something in the clips array before un-commenting this line
// AudioSource.PlayClipAtPoint (clips [0], Camera.main.transform.position);
// CUT THE TILE
GrowTileCombo = 0;
TileBounds.x -= Mathf.Abs(deltaX);
if (TileBounds.x <= 0)
return false;
float middle = LastTilePos.x + t.localPosition.x / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
CreateTileCutOff
(
new Vector3((t.position.x > 0)
? t.position.x + (t.localScale.x / 2)
: t.position.x - (t.localScale.x / 2)
, t.position.y
, t.position.z),
new Vector3(Mathf.Abs(deltaX), 1, t.localScale.z)
);
t.localPosition = new Vector3(middle - (LastTilePos.x / 2), ScoreCount, LastTilePos.z);
}
else
{
// Put something in the clips array before un-commenting this line
// AudioSource.PlayClipAtPoint (clips [1], Camera.main.transform.position);
if (GrowTileCombo > AmountForGrowTileCombo)
{
TileBounds.x += TileSizeGain;
if (TileBounds.x > TileBoundSize)
TileBounds.x = TileBoundSize;
float middle = LastTilePos.x + t.localPosition.x / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
t.localPosition = new Vector3(middle - (LastTilePos.x / 2), ScoreCount, LastTilePos.z);
}
GrowTileCombo++;
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, LastTilePos.z);
}
}
else
{
float deltaZ = LastTilePos.z - t.position.z;
if (Mathf.Abs(deltaZ) > PlayerErrorMargin)
{
// Put something in the clips array
// AudioSource.PlayClipAtPoint (clips [0], Camera.main.transform.position);
// CUT THE TILE
GrowTileCombo = 0;
TileBounds.y -= Mathf.Abs(deltaZ);
if (TileBounds.y <= 0)
return false;
float middle = LastTilePos.z + t.localPosition.z / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
CreateTileCutOff
(
new Vector3(t.position.x
, t.position.y
, (t.position.z > 0)
? t.position.z + (t.localScale.z / 2)
: t.position.z - (t.localScale.z / 2)),
new Vector3(t.localScale.x, 1, Mathf.Abs(deltaZ))
);
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, middle - (LastTilePos.z / 2));
}
else
{
// Put something in the clips array
// AudioSource.PlayClipAtPoint (clips [1], Camera.main.transform.position);
if (GrowTileCombo > AmountForGrowTileCombo)
{
if (TileBounds.y > TileBoundSize)
TileBounds.y = TileBoundSize;
TileBounds.y += TileSizeGain;
float middle = LastTilePos.z + t.localPosition.z / 2;
t.localScale = new Vector3(TileBounds.x, 1, TileBounds.y);
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, middle - (LastTilePos.z / 2));
}
GrowTileCombo++;
t.localPosition = new Vector3(LastTilePos.x, ScoreCount, LastTilePos.z);
}
}
SecondaryTilePos = (isMovingOnX)
? t.localPosition.x
: t.localPosition.z;
isMovingOnX = !isMovingOnX;
return true;
}
private void ColorMesh(Mesh mesh)
{
Vector3[] vertices = mesh.vertices;
Color32[] colors = new Color32[vertices.Length];
colorTransition += 0.1f;
if (colorTransition > 1)
{
colorTransition = 0.0f;
startColor = endColor;
int ci = lastColorIndex;
while (ci == lastColorIndex)
ci = Random.Range(0, TileColours.Length);
endColor = TileColours[ci];
}
Color c = Color.Lerp(startColor, endColor, colorTransition);
for (int i = 0; i < vertices.Length; i++)
colors[i] = c;
mesh.colors32 = colors;
}
private void EndGame()
{
if (PlayerPrefs.GetInt("score") < ScoreCount)
PlayerPrefs.SetInt("score", ScoreCount);
GameOver = true;
// endPanel.SetActive(true);
TheTowerStack[TowerStackIndex].AddComponent<Rigidbody>();
}
public void OnButtonClick(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
Comment
Your answer
Follow this Question
Related Questions
If statement not getting called after Lerping a Scale 1 Answer
Scale a plane without scaling or movintg it's texture 3 Answers
What scale value is necessary for get 1\2 part of the original? 1 Answer
Spawning multiple gameobjects with random scales 2 Answers
My GameObject disappears when running this script. 0 Answers