If there is a mirror laser ignores other objects - c#

I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects
Please can anyone help me, why this is happening.
I can't ahre this because "It's all code and stuff".
public class LaserBeam : MonoBehaviour {
LineRenderer lr;
public bool isOpen = true;
Vector3 s;
void Start () {
lr = GetComponent<LineRenderer>();
}
void Update () {
s = new Vector3(transform.position.x, transform.position.y + (2 / 5f), transform.position.z);
lr.SetPosition(0, s);
lr.SetWidth(0.3f, 0.3f);
if (isOpen)
{
RaycastHit[] Hit = Physics.RaycastAll(s, transform.forward, 100.0F);
//Debug.Log("isOpen W");
if (Hit.Length > 0)
{
for (int x = 0; x < Hit.Length; x++)
{
Debug.Log(Hit[x].collider.tag + " ID: " + x);
if (Hit[x].collider.tag == "Mirror" || !Hit[x].collider.isTrigger)
{
Debug.DrawLine(s, Hit[x].point, Color.blue);
lr.SetPosition(1, Hit[x].point);
// Debug.Log("loop W" + x);
if (Hit[x].collider.tag == "Mirror") Reflect(s, Hit[x], 0);
else lr.SetVertexCount(2);
break;
}
else if (x == Hit.Length - 1)
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
break;
}
}
}
else
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
}
}
else
{
lr.SetVertexCount(2);
lr.SetPosition(1, s);
}
}
public void Reflect(Vector3 start, RaycastHit hit, int id)
{
lr.SetVertexCount(id + 3);
Vector3 p = Vector3.Reflect(hit.point - start, hit.normal);
Debug.DrawRay(hit.point, hit.normal * 3);
Debug.DrawLine(hit.point, p + hit.point, Color.blue);
RaycastHit[] Hit1 = Physics.RaycastAll(hit.point, p, 100.0F);
if (Hit1.Length > 0)
{
for (int x = 0; x < Hit1.Length; x++)
{
if (Hit1[x].collider.tag == "Mirror" || !Hit1[x].collider.isTrigger)
{
Debug.DrawLine(hit.point, Hit1[x].point, Color.blue);
//Debug.DrawLine(hit.point, Hit[x].point, Color.blue);
//lr.SetPosition(id + 1,(hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Hit1[x].point);
if (Hit1[x].collider.tag == "Mirror")
{
Reflect(hit.point, Hit1[x], (id + 1));
return;
}
else lr.SetVertexCount(id + 3);
return;
}
else if (x == Hit1.Length - 1)
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
return;
}
}
else
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
// Debug.Log(id);
}
}
I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects
Please can anyone help me, why this is happening.
I can't ahre this because "It's all code and stuff".

I don't think you can get away with layers in this case, but you DO NEED to know how to use them, anyways, this should work:
if (Hit.Length > 0)
{
float firstHitDistance = 100000f; //or something ridiculously high
RaycastHit firstHit;
for (int x = 0; x < Hit.Length; x++)
{
if(Hit[x].distance < firstHitDistance){
firstHitDistance = Hit[x].distance;
firstHit = Hit[x];
}
}
if (firstHit.collider.tag == "Mirror" || !firstHit.collider.isTrigger)
{
Debug.DrawLine(s, firstHit.point, Color.blue);
lr.SetPosition(1, firstHit.point);
// Debug.Log("loop W" + x);
if (firstHit.collider.tag == "Mirror") Reflect(s, , 0);
else lr.SetVertexCount(2);
break;
}
}
sorry for bad formatting, I'm on windows :/

Related

Checking if value in array is equal to a value not working

My goal for my code is to create an array of sprite in game, but 10 of the sprites are bombs. In which currently it does not do that. Currently, my if statement (I don't think) is not check for all possible guesses to get all 10 bombs.
foreach(Vector3 b in BombCords)
{
if (n && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f) {
g = Instantiate(Bomb1, b, new Quaternion(0, 0, 0, 0));
n = false;
Debug.Log("Bomb");
t = true;
break;
}
else if (n == false && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f) {
g = Instantiate(Bomb2, b, new Quaternion(0, 0, 0, 0));
n = true;
Debug.Log("Bomb");
t = true;
break;
}
// Debug.Log(b.x + " = " + Mathf.Round(i * 10.0f) * 0.1 + ", " + Mathf.Round(b.y * 10.0f) * 0.1f + " = " + Mathf.Round(l * 10.0f) * 0.1);
}
Right now their is 8 or 9 bombs coming up, and rarely is 10. I know that it is not instantiating the entirety 10 bombs because my Debug.Log("BOMB") only outputs the amount of bombs on the map.
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using UnityEngine.EventSystems;
public class BlockStuff : MonoBehaviour {
public GameObject Floor1;
public GameObject Floor2;
public GameObject Bomb1;
public GameObject Bomb2;
public GameObject Mined1;
public GameObject Mined2;
public Vector3[] BombCords = new Vector3[10];
void Update() {
var mousePos = Input.mousePosition;
var mousePosWorld = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D raycast = Physics2D.Raycast(mousePosWorld, new Vector2(0, 0), 1);
RaycastHit2D braycast = Physics2D.Raycast(mousePosWorld + Vector3.down, Vector2.zero, 0);
if (Input.GetMouseButtonDown(0)) {
if (raycast.collider.gameObject.layer == 3) {
GameObject g = Mined2;
if (BombCords.Contains(new Vector3(Mathf.RoundToInt(mousePosWorld.x - 0.5f) + 0.5f, Mathf.RoundToInt(mousePosWorld.y - 0.5f) + 0.5f, 0))) {
Debug.Log("BOMB");
}
if (raycast.collider.gameObject.tag == "Floor1") {
g = Instantiate(Mined1, raycast.collider.gameObject.transform.position, raycast.collider.gameObject.transform.rotation);
raycast.collider.gameObject.SetActive(false);
} else {
g = Instantiate(Mined2, raycast.collider.gameObject.transform.position, raycast.collider.gameObject.transform.rotation);
raycast.collider.gameObject.SetActive(false);
}
g.name = "grass";
g.transform.parent = transform;
}
// raycast.collider.gameObject.transform == Mathf.RoundToInt(raycast.collider.gameObject.transform.position.x) ; //IN PROCESS OF CHANGING GRASS TO MINE FLOOR
}
}
void Start() {
bool n = true;
bool t = false;
for (int i = 0; i < 10; i++) {
var BombCord = new Vector3(Mathf.RoundToInt(Random.Range(-9.5f, 0.5f) - 0.5f) + 0.5f, Mathf.RoundToInt(Random.Range(-4f, 5f) - 0.5f) + 0.5f, -0.2f);
// for (int l = 0; l <= i; l++){
foreach (Vector3 b in BombCords) {
while (true) {
if (b == BombCord) {
BombCord = new Vector3(Mathf.RoundToInt(Random.Range(-9.5f, 0.5f) - 0.5f) + 0.5f, Mathf.RoundToInt(Random.Range(-4f, 5f) - 0.5f) + 0.5f, -0.2f);
} else {
break;
}
}
}
BombCords[i] = BombCord;
Debug.Log(BombCords[i]);
// Instantiate(Bomb1, BombCord, new Quaternion(0,0,0,0));
}
for (float l = -4.5f; l < 5.5; l += 1) {
for (float i = -10.5f; i < 0.5; i += 1) {
GameObject g = Floor1;
foreach (Vector3 b in BombCords) {
if (n && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f) {
g = Instantiate(Bomb1, b, new Quaternion(0, 0, 0, 0));
n = false;
Debug.Log("Bomb");
t = true;
break;
} else if (n == false && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f) {
g = Instantiate(Bomb2, b, new Quaternion(0, 0, 0, 0));
n = true;
Debug.Log("Bomb");
t = true;
break;
}
// Debug.Log(b.x + " = " + Mathf.Round(i * 10.0f) * 0.1 + ", " + Mathf.Round(b.y * 10.0f) * 0.1f + " = " + Mathf.Round(l * 10.0f) * 0.1);
}
if (n && t == false) {
g = Instantiate(Floor1, new Vector3(i, l, -0.2f), new Quaternion(0, 0, 0, 0));
n = false;
} else if (n == false && t == false) {
g = Instantiate(Floor2, new Vector3(i, l, -0.2f), new Quaternion(0, 0, 0, 0));
n = true;
}
if (t) {
g.name = "BOMB";
} else {
g.name = "grass";
}
g.transform.parent = transform;
t = false;
// if (n && BombCords.Contains(new Vector3(i,l,-0.2f))){
// g = Instantiate(Bomb1, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = false;
// Debug.Log("Bomb");
// }
// else if (n == false && BombCords.Contains(new Vector3(i,l,-0.2f))){
// g = Instantiate(Bomb2, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = true;
// Debug.Log("Bomb");
// }
// else if (n && BombCords.Contains(new Vector3(i,l,-0.2f)) == false){
// g = Instantiate(Floor1, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// Debug.Log(i + " " + l + " " + g.transform.position);
// n = false;
// }
// else if (n == false){
// g = Instantiate(Floor2, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = true;
// }
}
}
}
}
I'm sorry if my title isn't right, if it is, please tell me some suggestions to change it to
,thanks

cannot convert from UnityEngine.Vector3 to System.Collections.Generic.List<UnityEngine.Vector3>

I'm making a total war demo game where you drag on the screen to move your units. This bug has been bothering me and I don't know what's the reason behind it. The error I get is below.
FormationScript.cs(119,44): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'System.Collections.Generic.List<UnityEngine.Vector3>'
Here is the code. (sorry for its length)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class FormationScript : MonoBehaviour
{
public GameObject[] units;
public Transform formationBarrier;
float unitWidth = 2.0f; // This also includes the gap between them, in this case the unit width is 1 and the gap is also 1
private float numberOfUnits;
private double unitsPerRow;
private float numberOfRows;
Vector3 startClick;
Vector3 endClick;
Vector3 selectedAreaSize;
Vector3 selectedAreaPos;
float startMinusEndX;
float startMinusEndZ;
private List<List<Vector3>> availablePositions;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Formation();
}
void Formation()
{
if (Input.GetMouseButtonDown(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
startClick = ray.GetPoint(distance);
}
Debug.Log(startClick);
}
if (Input.GetMouseButtonUp(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
endClick = ray.GetPoint(distance);
}
Debug.Log(endClick);
}
// ======================================================================================================
/*if (startClick.x - endClick.x < 0 || startClick.z - endClick.z < 0)
{
startMinusEndX = startClick.x + endClick.x;
startMinusEndZ = startClick.z + endClick.z;
}
else
{
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
}
if (startMinusEndX > 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
}
else if (startMinusEndX < 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) * 2, 0, (startClick.z + endClick.z) / 2);
}
*/
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
// ======================================================================================================
selectedAreaSize = formationBarrier.localScale;
selectedAreaPos = formationBarrier.localPosition;
numberOfUnits = units.Length;
unitsPerRow = (unitWidth / numberOfUnits) * selectedAreaSize.x;
unitsPerRow = Math.Round(unitsPerRow, 0);
if (unitsPerRow < 0)
{
unitsPerRow = unitsPerRow * -2;
}
if (numberOfRows % 1 == 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
for (int i = 0; i > numberOfRows; i++) // i is the number of rows
{
//availablePositions.Add(i);
for (int j = 0; j > numberOfUnits / unitsPerRow; j++) // j is the number of units / the units per row
{
Vector3 pos = new Vector3((selectedAreaPos.x / ((float)unitsPerRow + 1)) + ((j - 1) * (selectedAreaPos.x / ((float)unitsPerRow + 1))), 0.0f, i * 2);
availablePositions.Add(pos); // Heres where the error's coming from
}
}
}
else if (numberOfUnits % (float)unitsPerRow != 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
}
Debug.Log(unitsPerRow);
Debug.Log(numberOfRows);
}
}
I am pretty new to Unity so go easy : )
There is a syntax error in your code. You are inserting pos which is of type Vector3 into availablePositions, which is a List of List of Vecotr3.
Either change availablePositions definition:
private List<Vector3> availablePositions;
or convert pos to list before adding to availablePositions:
availablePositions.Add(new List<Vector3>{pos});

Car control with Swipe(including No Physics)

I want to make a code with endless 3d car game, car control by swipe left and right. without using any physics.
info.
3-lane,specific lane center (9,0,car z position),(0,0,car z position),(-9,0,car z position).
I do swipe control and move swipe control but not good.
public class playercontrol: MonoBehaviour {
public void Update() {
playera.transform.Translate((Vector3.forward * speed * Time.deltaTime));
Debug.Log("speed" + speed);
if (check == false) {
if (Input.GetMouseButtonDown(0)) {
Debug.Log("left button");
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButtonUp(0)) {
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
//swipe left
if (currentSwipe.x < -1.0 f && currentSwipe.y > -0.5 f && currentSwipe.y < 0.5 f) {
Debug.Log("left swipe");
if (transform.position.x > -9 f) {
spos = transform.position;
Debug.Log("spos" + spos);
endpos = new Vector3(transform.position.x - 9 f, transform.position.y, transform.position.z + ((speed * 2) / 20));
Debug.Log("speed......... ......." + ((speed * 5) / 20));
transform.rotation = Quaternion.Euler(0, -10.0 f, 0);
check = true;
//endpos * (Time.deltaTime*0.5f);
// Time.timeScale = 0.5f;
// to.rotation = Quaternion.Euler (0, 0, 0);
}
}
//swipe right
if (currentSwipe.x > 1.0 f && currentSwipe.y > -0.5 f && currentSwipe.y < 0.5 f) {
Debug.Log("right swipe");
if (transform.position.x < 9 f) {
Debug.Log("spos1" + spos);
spos = transform.position;
endpos = new Vector3(transform.position.x + 9 f, transform.position.y, transform.position.z + ((speed * 2) / 20));
Debug.Log("speed1......... ......." + ((speed * 5) / 20));
transform.rotation = Quaternion.Euler(0, 10.0 f, 0);
check = true;
// Time.timeScale = 0.5f;
// to.rotation = Quaternion.Euler (0, 0, 0);
}
}
}
}
if (check == true) {
time += 10 * Time.deltaTime;
transform.position = Vector3.Lerp(spos, endpos, time);
// transform.rotation = Quaternion.Lerp (from.rotation, to.rotation, time);
if (time > 1) {
transform.rotation = Quaternion.Euler(0, 0, 0);
//Time.timeScale = 1.0f;
//playera.transform.Rotate (0, 0, 0);
//transform.Rotate (0, 0, 0);
check = false;
time = 0;
}
}
}
}

How can i convert a game with multiple scene into a single scene game?

I have made a game in unity which have multiple scenes of different level in which user have to select a colored block to go the next level but i want my code to be in a single scene is there a way i can convert my multi scene game into a single scene .
Code for level 1 grid base is
public IEnumerator Start() {
grid = new GameObject[ySize, xSize];
float x = xStart;
float y = yStart;
ScreenRandom = Random.Range(0, 3);
if (ScreenRandom == 0)
{
img.color = UnityEngine.Color.red;
text.text = "Select all the Red Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountRed++;
}
else {
GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 1)
{
img.color = UnityEngine.Color.blue;
text.text = "Select all the Blue Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountBlue++;
}
else {
GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 2)
{
img.color = UnityEngine.Color.yellow;
text.text = "Select all the Yellow Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountYellow++;
}
else {
GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
}
Code to check and Load Level 2 is:
void Update () {
screen = GridControl.ScreenRandom;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (screen == 0)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "RedBlock")
{
RedCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (RedCount == GridControl.CountRed)
{
print("Next Level");
Application.LoadLevel(3);
}
}
if (hit.collider.tag == "YellowBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
}
}
if (screen == 1)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
BlueCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (BlueCount == GridControl.CountBlue)
{
print("Next Level");
Application.LoadLevel(3);
}
}
if (hit.collider.tag == "RedBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "YellowBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
}
}
if (screen == 2)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "RedBlock")
{
Destroy(hit.transform.gameObject);
print("Game Over");
Application.LoadLevel(0);
}
if (hit.collider.tag == "YellowBlock")
{
YellowCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (YellowCount == GridControl.CountYellow)
{
print("Next Level");
Application.LoadLevel(3);
}
}
}
Code for Level 2 GridBase is;
public void Start()
{
grid = new GameObject[ySize, xSize];
ScreenRandom = GridControl.ScreenRandom;
float x = xStart;
float y = yStart;
if (ScreenRandom == 0)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountRed++;
}
else {
GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 1)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountBlue++;
}
else {
GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 2)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountYellow++;
}
else {
GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
}
To start I don't know why would you do that. Technically that is the whole point of having scenes. But still what you could do is put everything (Level 1 and 2) in one scene and then position and disable whatever belongs to Level 2 outside the camera. Then instead of calling Application.LoadLevel I would call a function that will move everything that belongs to Level 1 out of the screen and at the same time moves Level 2 into the screen. If I where you I would group all the objects of one level as child of another GameObject (maybe called LevelX), that way it will be easier to move everything at once. This will even give you nice slide in transition between levels. If you don't want the transition just change the positions instantly.
Be carefull because depending on the amount of levels you could have serious performance issues.
You could use a prefab for the colored blocks and drag it into your different level scenes. If you now attach your behaviour script (managing what happens if a raycast hits it) to your prefab, all instances in all levels will have the same logic. That way you can reuse the same code base for different levels.

Bouncing Ease Equation in C#

I'm trying to add a bounce effect to an element of a game I'm making using C#. I can't seem to find the correct easing equation for this. Here's what I'm using for the time being:
t.position += (destination-t.position)*0.05f;
if((destination-t.position).magnitude <= 0.01f)
{
t.position = destination;
}
Can anyone help me with changing it to a bouncing equation?
if(setUpEase)
{
// Set the destination of each boardspace
if(!oneTime)
{
beginning = new Vector3(t.position.x, t.position.y, t.position.z);
destination = new Vector3(t.position.x, t.position.y-10, t.position.z);
change = new Vector3(0, -10, 0);
setUpDuration = Random.Range (1.0f, 3.0f);
oneTime = true;
}
// BOUNCING
currentTime += Time.deltaTime;
float t2 = currentTime/setUpDuration;
if(currentTime < setUpDuration)
{
if (t2 < (1/2.75f))
{
t.position = change*(7.5625f*t2*t2) + beginning;
}
else if (t2 < (2/2.75f))
{
t.position = change*(7.5625f*(t2-=(1.5f/2.75f))*t2 + 0.75f) + beginning;
}
else if (t2 < (2.5f/2.75f))
{
t.position = change*(7.5625f*(t2-=(2.25f/2.75f))*t2 + 0.9375f) + beginning;
}
else
{
t.position = change*(7.5625f*(t2-=(2.625f/2.75f))*t2 + 0.984375f) + beginning;
}
}
else
{
t.position = destination;
if(boardId < 113 && boardId > 92)
pie.setUpEase2 = true;
setUpEase=false;
}
}

Categories

Resources