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:
- If movement key is pressed–
- Check current direction.
- Then, define current position.
- Then define new position based on direction.
- Define position and current position based upon array.
- Check the array for a 1 or a 0; 1 means it is a wall.
- 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.