- Home /
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:
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.
Well, you declare a local variable in Start() called map, then you try and access it outside of it's local scope.
I tried to make it public, that causes a host of new problems like Array out of range. I don't really understand what is going on here. Can someone provide a more detailed answer on how to go about this please? As I said before, I already went though multiple tutorials, videos, the Unity documentation, the wiki, etc. I shouldn't have to repeat myself. If you don't know what I am ai$$anonymous$$g for, here is a link to Eye of the Beholder on Youtube:
Answer by Jeff-Kesselman · Jun 05, 2014 at 03:23 PM
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.
Sorry, but you misunderstand. I want it to jump. I don't want to use the physics because they cause weird things to my player object (such as tipping over when hitting a wall, going through walls with incorrect colliders, walls being pushed, etc). In addition, I cannot use grid movement with the physics either. Thus I am using translate. For this to work, I need to use my own code. Everything works except for collisions, and they didn't work how I wanted them with a player controller. Take a look at Legend of Grimrock and you will see the type of movement I am trying to simulate (just without the smooth transition between cells).
Never$$anonymous$$d, I finally figured it out! Collisions are working after months of trial and error. I am so ecstatic right now. I didn't realize I was so close to figuring it out. Thank you to everyone who tried to help me.