The game is based on ice, a hockey puck will slide in one direction until it ‘hits’ something or reaches the edge of the level.
Basically, the code will spawn objects on a little grid, and use iTween to move around. The code logically should ensure that if you are meant to collide with one of the obstacles, it will return the right coords for iTween to use so that it doesn’t go through it or end up inside it and will instead put you next to it. It did work, but now it doesn’t work at all and you always go through the objects.
So far as I can tell, the array is fine, all the methods are fine, but the if statement is never launched during the for loop that checks whether there are any obstacles between the current position and hypothetical new position.
using UnityEngine;
using System.Collections;
public class MainScript : MonoBehaviour {
//************************************VARIABLE DECLARATIONS************************************
GameObject puck;
GameObject cube;
GameObject icecube;
Hashtable ht = new Hashtable();
bool buttonsEnabled = true;
int gridSize;
bool[,] obstacles;
bool[,] objectives;
//************************************INITIALISATION************************************
void Start ()
{
gridSize = 5;
obstacles = new bool[gridSize,gridSize];
objectives = new bool[gridSize,gridSize];
puck = GameObject.Find("Puck");
cube = GameObject.Find("Cube");
icecube = GameObject.Find("IceCube");
GameObject.Destroy(icecube);
obstacles[1,4] = true;
obstacles[3,2] = true;
obstacles[0,3] = true;
for (int i = 0; i < gridSize; i++)
{
for (int j = 0; j < gridSize; j++)
{
if (obstacles[i,j] != true)
obstacles[i,j] = false;
objectives[i,j] = false;
}
}
objectives[4,0] = true;
objectives[2,1] = true;
spawnObstacles();
}
//************************************GAME METHODS************************************
//Movement method - converts user input to movements
void FixedUpdate()
{
if (buttonsEnabled == true)
{
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
iTween.MoveTo (puck, getHT("-x"));
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
iTween.MoveTo(puck, getHT("x"));
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
iTween.MoveTo(puck, getHT("z"));
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
iTween.MoveTo(puck, getHT("-z"));
}
}
}
//Movement method - build hashtable args for iTween movement methods
public Hashtable getHT(string direction)
{
Hashtable ht = new Hashtable();
Vector3 newPos = puck.transform.position;
if (direction == "x")
{
newPos.x = 4;
}
else if (direction == "-x")
{
newPos.x = 0;
}
else if (direction == "z")
{
newPos.z = 4;
}
else if (direction == "-z")
{
newPos.z = 0;
}
ht.Add("position", moveToNewPos(puck.transform.position, newPos, direction));
ht.Add("time", 1.5f);
ht.Add("onstart", "disableKeys");
ht.Add("oncomplete", "enableKeys");
return ht;
}
//Movement method - disables keys while puck is moving
public void disableKeys()
{
buttonsEnabled = false;
}
//Movement method - enables keys when puck stops moving
public void enableKeys()
{
buttonsEnabled = true;
}
//Movement method - checks for obstacles between initial and final position and returns appropriate coords
public Vector3 moveToNewPos(Vector3 pos, Vector3 newPos, string direction)
{//initialise firstObstacle to pos so that only one axis need be set
Vector3 firstObstacle = pos;
if (direction == "x")
{
for (int i = (int)pos.x; i < 5; i++)
{
if (obstacles[i, (int)pos.z] == true)
{
firstObstacle.x = i - 1;
return firstObstacle;
}
}
}
else if (direction == "-x")
{
for (int i = (int)pos.x; i > 0; i--)
{
if (obstacles[i, (int)pos.z] == true)
{
firstObstacle.x = i + 1;
return firstObstacle;
}
}
}
else if (direction == "z")
{
for (int i = (int)pos.z; i < 5; i++)
{
if (obstacles[(int)pos.x, i] == true)
{
firstObstacle.z = i - 1;
return firstObstacle;
}
}
}
else if (direction == "-z")
{
for (int i = (int)pos.z; i > 0; i--)
{
if (obstacles[(int)pos.x, i] == true)
{
firstObstacle.z = i + 1;
return firstObstacle;
}
}
}
return newPos;
}
//Environment method - creates and spawns obstacles based on coordinates supplied by true values in obstacles array
public void spawnObstacles()
{
Vector3 pos = new Vector3();
Quaternion r = new Quaternion();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (obstacles[i,j] == true)
{
obstacles[i,j] = false;
pos.x = i;
pos.z = j;
pos.y = -0.35f;
Instantiate(cube, pos, r);
}
else if (objectives[i,j] == true)
{
objectives[i,j] = false;
pos.x = i;
pos.z = j;
pos.y = 0;
Instantiate(icecube, pos, r);
}
}
}
}
}