How to create a UnityScript array and access the data in each cell.

Hello everyone. I am not much of a coder but more of an artist. However, I do not know any coders so I have to do this myself. I am trying to make a game like Eye of the Beholder, Wizardry, Etrian Odyssey, Legend of Grimrock, and Might and Magic X. These games are called first person dungeon crawlers (for anyone who never played games in the 80’s and 90’s like I have). I have been spending time with Unity for over a year and I partially understand the syntax for UnityScript. I have successfully gotten 3d grid movement in a first person environment with an internal compass. However, what has stumped me is collisions.

I have been struggling with collisions for months upon months upon months now, trying to figure out how to get it to work. I don’t want the player to go through walls or other objects. I discovered that Translate ignores collisions, even if you have an OnTrigger event. So now I have been trying to figure out how to use an array. However, I am totally stumped. I have looked online through forums, tutorials, videos, the Unity documentation and wiki, etc. for an answer but no one actually tells you how to do this. I know it’s possible because Might and Magic X from what I heard was made in Unity. I normally don’t post questions or post on forums but I am totally stuck and this is a last resort. I have no idea on what to do or how to do it. I need a very detailed answer on how to go about doing this.

Now here is the logic I am trying to go for:

  1. If movement key is pressed–
  2. Check current direction.
  3. Then, define current position.
  4. Then define new position based on direction.
  5. Define position and current position based upon array.
  6. Check the array for a 1 or a 0; 1 means it is a wall.
  7. If new position is not 1, movement takes place.

The list items in italics is the part I am having problems with. Everyone online says use an array, but no explains how to do it. I have tried For loops, but those don’t work or I haven’t gotten them to work because I don’t know how to assign values to each cell after the loop is completed. I have been struggling with this for months upon months and cannot figure it out. I need a very detailed answer on how to do this.

Below is my code so anyone can have a better understanding of what I am trying to do:

#pragma strict

var moveSpeed:int = 1;
var rotateSpeed:int = 90;
var direction:int;
var directionName:String;

function Start () {

	var map = [
		[1,1,1,1,1],
		[1,0,0,0,1],
		[1,0,1,0,1],
		[1,0,0,0,0],
		[1,1,1,1,1]
		];
	
	direction = 1;
	Direction();
}

function Update () {
	if (Input.GetKeyDown(KeyCode.W)) {
		if (direction == 1) {
			var curPos = transform.position;
			var newPos = map[curPos.z + 1][curPos.x];
			if (newPos != 1) {
				transform.position += transform.forward * moveSpeed;
			}
		}
	}
	
	if (Input.GetKeyDown(KeyCode.S)) {
		transform.position -= transform.forward * moveSpeed;
	}
	
	if (Input.GetKeyDown(KeyCode.A)) {
		transform.position -= transform.right * moveSpeed;
	}
	
	if (Input.GetKeyDown(KeyCode.D)) {
		transform.position += transform.right * moveSpeed;
	}
	
	if (Input.GetKeyDown(KeyCode.E)) {
		if (direction < 4) {
			direction += 1;
		}
		else if (direction == 4){
			direction = 1;
		}
		Direction();
		transform.Rotate(0, rotateSpeed, 0);
	}
	
	if (Input.GetKeyDown(KeyCode.Q)) {
		if (direction == 1) {
			direction = 4;
		}
		else if (direction > 1){
			direction -= 1;
		}
		Direction();
		transform.Rotate(0, -rotateSpeed, 0);
	}
	
}

function Direction () {
	switch (direction) {
		case 1:
			directionName = "North";
			break;
		case 2:
			directionName = "East";
			break;
		case 3:
			directionName = "South";
			break;
		case 4:
			directionName = "West";
			break;
	}
}

I keep getting BCE0048 errors or need semicolon errors when attempting to access the array, thus telling me what I am trying to do is not working. I hope someone can help me. I would greatly appreciate it. Again, I need a detailed answer on how to do this. Thank you very much to anyone who can help me.

I could debug your code, but the fact of the matter is that you are off in a very bad direction.

Your problem is not that Collision doesn’t work but rather that you are doing movement wrong.

When you just add or subtract from your position, its like taking the object out of the world where it and putting it back in the new place without going through the intervening distance.

Instead you want to move by either setting your velocity to your speed, or applying a force to do that, (Setting velocity is more likely what you want for a player controlled character.)

Also, make sure that every object that yo want to be “solid” has a collider, and that every solid object this is moving has a rigid body.

Have you looked at some of the Unity tutorial projects? They might help you see how Unity really wants things done.

P.S. I strongly recommend you make the jump to C#, UnityScript (what people here call Javascript, but isn’t really) does not give you the kind of help finding your problems that C# does. Especially for bigger projects, C# is a pretty clear win.