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 coolbird22 · Sep 01, 2014 at 06:45 PM · collisionarraygrid

How to create a 2D grid based array with passable/impassable grids for movement ?

How can I create a 2D array that allows movement through only the lanes, but not the dark grey blocks as in the picture below ? I tried using rigidbody and colliders and the result is plain terrible, as the sphere resembles a woodpecker drilling into a branch everytime there is a collision.

Thanks for taking a look into this.

alt text

The code that I'm using to move in a grid like fashion is given below:

 using UnityEngine;
 using System.Collections;
 
 public class ControllerScript : MonoBehaviour {
     public float speed = 1.0f;
     
     private Vector3 endpos;
     private bool moving = false;
     
     void Start () {
         endpos  = transform.position;
     }
     
     void Update () {
         if (moving && (transform.position == endpos))
             moving = false;
         
         if(!moving && Input.GetKey(KeyCode.W)){
             moving = true;
             endpos = transform.position + Vector3.up;
         }
 
         if(!moving && Input.GetKey(KeyCode.S)){
             moving = true;
             endpos = transform.position + Vector3.down;
         }
 
         if(!moving && Input.GetKey(KeyCode.A)){
             moving = true;
             endpos = transform.position + Vector3.left;
         }
 
         if(!moving && Input.GetKey(KeyCode.D)){
             moving = true;
             endpos = transform.position + Vector3.right;
         }
         
         transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
     }
 }

tt.png (16.8 kB)
Comment
Add comment · Show 8
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 robertbu · Sep 01, 2014 at 10:09 PM 0
Share

Assu$$anonymous$$g you built your grid so all the lanes are on unit boundaries, and the sphere starts in a lane, there should not be any collisions.

avatar image bubzy · Sep 01, 2014 at 10:47 PM 0
Share

as robertbu said. you may have a scaling issue, however if you did want to go down the array route its something like

 pseudocode:

 //where x is an illegal square

 mapArray[5][5] = o,x,o,x,o,
                  o,x,o,x,o,
                  o,x,o,x,o,
                  o,x,o,x,o,
                  o,x,o,x,o
 
 Vector2 playerPos (0,0)
 
 
 moveFunction()
 {
 Vector2 oldPos = playerPos;
 if{inputUp}
 {
 playerPos += Vector2(0,1);
 }
 if left, if right, if down etc....
 
 //check player position in array
 
 if(mapArray[playerPos.x][playerPos.y]!='x')
 {
 //do transformy stuff
 }
 else
 {
 playerPos = oldPos;
 }
 
 }


or something :D

avatar image coolbird22 · Sep 02, 2014 at 05:54 PM 0
Share

@robertbu + @bubzy - All the tiles are 1 unit of 100 pixels each, so they are aligned to a grid pretty much. Building the level is the only thing I can do so far and would require some more guidance to make it work. How do I make the 2D array correspond to the level ? How do I connect the array to the level and tell the sphere object to not move even if I'm pressing the button to make it go into the direction of a wallblock ? Although @bubzy provided a rough code, I'm having a hard time understanding it. A deeper explanation as to how this works or links to resources that better explain this would be very helpful. Thanks for taking the time to reply.

The below is the kind of array that I wish to create, with 1 being unwalkable.

 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

avatar image robertbu · Sep 02, 2014 at 06:53 PM 0
Share

Did you take the Rigidbody off the ball? Given your code, without a Rigidbody, I don't see how the ball is colliding with a tile. Your code only allows one movement (horizontal or vertical), and it locks out other movement until the move is complete. As long as you clamp the ball so that it cannot go outside, I don't see a way for a collision to occur.

avatar image coolbird22 · Sep 02, 2014 at 07:45 PM 0
Share

Yes, I did take the rigidbody off the ball since this type of grid avoidance doesn't require physics. It is true that until the movement is complete, other movement is locked out. When I'm adjacently onto the left of the wall block and when I press D, the sphere moves on top of the wall block tile. This is exactly what I wish to fix. How do I clamp the movement of the back so that it never enters a wall block tile?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by coolbird22 · Sep 18, 2014 at 04:55 PM

@bubzy replied to the question with an example. Going through it all made a lot of sense and helped a lot. Here is his reply.

I don't usually do this but http://westwalescse.net/unity/arrayDemo.rar take a look, theres a few annoying things with positioning and whatever, but the idea is there.

it really does look as though you could do with brushing up on your coding a little bit though, this is bread-and-butter logic :)

Comment
Add comment · 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

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

Keeping track of array objects 2 Answers

Moving cubes into a grid formation 1 Answer

Ignore Colliders of an Object 1 Answer

Moving a character into a 2d int array 0 Answers

Visualize array in game 2 Answers


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