HI all,
In my game I have a cube that rolls from grid to grid. The grid is 9x9 squares. On each grid is a script attached that has two boolean values; hasLightOn and isOccupied. Now when I spawn my obstacles onto a grid piece, the isOccupied changes to true.
I have attached a rather large piece of code that has my cube movement on it and I want to use either my stopCube function or try and utilise OnCollisionEnter()
How can I stop my cube moving onto a grid piece if the grid is occupied? How can I get the cube to read ahead before the movement is made. At the moment the cube rolls into the obstacles and bounces off violently
using UnityEngine;
using System.Collections;
public class cubeTracker : MonoBehaviour
{
private float x = 0;
private float y = 0;
private float z = 0;
public static int lightCount = 0;
public static int score = 0;
private Vector3 cPos;
private float speed = 2.5f;
private float size = 1.0f;
private bool flopping = false;
void Start()
{
lightCount = dataManager.lightsOnCount - dataManager.obstacleCount;
}
void Update()
{
Vector2 move = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (move.magnitude > 0.2)
{
StartCoroutine(Flop(move));
}
}
IEnumerator Flop(Vector2 movDir)
{
Vector3 pivot = new Vector3(0,0,0);
Vector3 dir = new Vector3(0,0,0);
if (flopping)
{
yield break; // ignore other commands while flopping
}
if (movDir.y > 0)
{
dir = Vector3.forward; // will flop forward
pivot = new Vector3(0,-1,1); // defines point around which rotate
flopping = true;
z++;
}
else if (movDir.y < 0)
{
dir = -Vector3.forward;
pivot = new Vector3(0,-1,-1);
flopping = true;
z--;
}
else if (movDir.x < 0)
{
dir = -Vector3.right;
pivot = new Vector3(-1,-1,0);
flopping = true;
x--;
}
else if (movDir.x > 0)
{
dir = Vector3.right;
pivot = new Vector3(1,-1,0);
flopping = true;
x++;
}
AlignBlock();
// calculates the point around which the block will flop
if (flopping)
{
pivot = transform.position + (pivot * size / 2);
var org = transform.position - pivot;
var dest = (transform.position + dir * size) - pivot;
var rot0 = transform.rotation;
var rot1 = Quaternion.FromToRotation(org, dest) * rot0;
float a = 0;
while (a < 1)
{
var dt = Time.deltaTime * speed;
a += dt;
transform.position = Vector3.Slerp(org, dest, a) + pivot;
transform.rotation = Quaternion.Lerp(rot0, rot1, a);
yield return null;
}
if (audio)
{
audio.Play();
}
flopping = false;
cPos = new Vector3(x,y,z);
checkCubeOnLight();
stopCube();
}
}
private void AlignBlock()
{
var angles = transform.eulerAngles;
// forces euler angles to be multiple of 90
angles.x = 90 * Mathf.RoundToInt(angles.x / 90);
angles.y = 90 * Mathf.RoundToInt(angles.y / 90);
angles.z = 90 * Mathf.RoundToInt(angles.z / 90);
transform.eulerAngles = angles;
var pos = transform.position;
// forces x and z to be in a grid
pos.x = size * Mathf.RoundToInt(pos.x / size);
pos.z = size * Mathf.RoundToInt(pos.z / size);
if (!rigidbody) pos.y = size * Mathf.RoundToInt(pos.y / size);
transform.position = pos;
}
private void checkCubeOnLight()
{
// if there are still lights on perform these actions
Transform cubeLight;
if (lightCount > 0)
{
for (short i=0; i< dataManager.lightsOnCount; i++)
{
if (cPos != (Vector3)dataManager.lgtPos[i])
{
// just keep checking
continue;
}
else
{
cubeLight = dataManager.listOfGrids[i].transform.FindChild("GridLight");
gridtile tile = dataManager.listOfGrids[i].gameObject.GetComponent<gridtile>();
if (cubeLight == null || tile == null)
{
Debug.Log("Light not found!");
break;
}
if (cubeLight.light.enabled == true)
{
cubeLight.light.enabled = false;
tile.haslightsOn = false;
lightCount--;
score += 10;
}
else
{
cubeLight.light.enabled = true;
tile.haslightsOn = true;
lightCount++;
score -= 5;
}
}
}
}
}
void stopCube()
{
if (lightCount > 0)
{
for (short i = 0; i < dataManager.listOfGrids.Count; i++)
{
// gridtile is the script attached to each grid piece
gridtile tile = dataManager.listOfGrids[i].gameObject.GetComponent<gridtile>();
}
}
}
}