I am just learning programming and thought I would create a simple game in Unity where one presses the arrow keys to create squares in the arrow key’s direction. I am trying to create a raycast that will detect if there is a square already in that direction, and if there is to not create a square. Here is my code so far:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class shuffle : MonoBehaviour
{
public LayerMask mask = -1;
public List<Vector3> spaces;
public GameObject[] cards;
public GameObject[] spacesToAdd;
public int timesTriggered;
public int number;
public GameObject card;
public GameObject space;
private GameObject lastSpaceInstantiated;
private GameObject firstSpace;
public GameObject spawnOnMe;
private bool first = true;
public float fireDelay = 1;
public float firePause = 1;
void Start()
{
}
void ShuffleRed()
{
spaces = new List<Vector3>();
cards = new GameObject[(GameObject.FindGameObjectsWithTag("cards").Length)];
spacesToAdd = new GameObject[(GameObject.FindGameObjectsWithTag("spaces").Length)];
for (int i = 0; i < spacesToAdd.Length; i++)
{
spacesToAdd[i] = (GameObject.FindGameObjectsWithTag("spaces"))[i];
};
for (int k = 0; k < spacesToAdd.Length; k++)
{
spaces.Add(spacesToAdd[k].transform.position);
};
for (int l = 0; l < cards.Length; l++)
{
cards[l] = (GameObject.FindGameObjectsWithTag("cards")[l]);
};
for (int m = 0; m < cards.Length; m++)
{
number = Random.Range(0, spaces.Count);
cards[m].transform.position = spaces[number];
spaces.RemoveAt(number);
};
}
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > firePause)
{
firePause = Time.time + fireDelay;
ShuffleRed();
}
if (Input.GetButton("Fire2") && Time.time > firePause)
{
firePause = Time.time + fireDelay;
Instantiate(card, transform.position, Quaternion.identity);
}
if ((Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right")) && Time.time > firePause && (GameObject.FindGameObjectsWithTag("spaces").Length) > (GameObject.FindGameObjectsWithTag("cards").Length - 1))
{
firePause = Time.time + fireDelay;
CreateSpace();
timesTriggered = timesTriggered + 1;
}
}
void CreateSpace()
{
if (Input.GetKey("up"))
{
if (Physics.Raycast(transform.position, transform.up, 3, mask.value) == false)
{
GameObject lastSpaceInstantiated = (GameObject)Instantiate(space, (spawnOnMe.transform.position + new Vector3(0, 2, 0)), Quaternion.identity);
spawnOnMe = lastSpaceInstantiated;
}
}
else if (Input.GetKey("down"))
{
if (Physics.Raycast(transform.position, -transform.up, 3, mask.value) == false)
{
GameObject lastSpaceInstantiated = (GameObject)Instantiate(space, (spawnOnMe.transform.position + new Vector3(0, -2, 0)), Quaternion.identity);
spawnOnMe = lastSpaceInstantiated;
}
}
else if (Input.GetKey("left"))
{
if (Physics.Raycast(transform.position, -transform.right, 3, mask.value) == false)
{
GameObject lastSpaceInstantiated = (GameObject)Instantiate(space, (spawnOnMe.transform.position + new Vector3(-2, 0, 0)), Quaternion.identity);
spawnOnMe = lastSpaceInstantiated;
}
}
else if (Input.GetKey("right"))
{
if (Physics.Raycast(transform.position, transform.right, 3, mask.value) == false)
{
GameObject lastSpaceInstantiated = (GameObject)Instantiate(space, (spawnOnMe.transform.position + new Vector3(2, 0, 0)), Quaternion.identity);
spawnOnMe = lastSpaceInstantiated;
}
}
}
}