Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RedVermillion · Jun 05, 2014 at 03:18 PM · javascriptscriptingproblembce0048collisionhandling

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Jun 05, 2014 at 03:20 PM 0
Share

Well, you declare a local variable in Start() called map, then you try and access it outside of it's local scope.

avatar image RedVermillion · Jun 05, 2014 at 04:34 PM 0
Share

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:

http://www.youtube.com/watch?v=rvyjaT767jk

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RedVermillion · Jun 05, 2014 at 03:46 PM 0
Share

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).

avatar image RedVermillion · Jun 06, 2014 at 01:11 PM 0
Share

/bump.

Anyone else want to take a crack at it?

avatar image RedVermillion · Jun 06, 2014 at 03:58 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[HELP] BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.GameObject'. 1 Answer

how to set the background image on slider 1 Answer

function OnCollosionEnter problems 1 Answer

UNITY3D: TEXTFILE I/O : Don't write same things 0 Answers

AI Shooting through walls. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges