Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Piflik · Sep 24, 2011 at 10:12 PM · collisiongridrealtimeturn-based

Grid based movement collision detection

I have a bit of a problem with my movement and I am not sure it is solvable at all...

I have player and enemies moving on a grid and I want them to not move into each other. I can't use physical colliders, since they then would stop somewhere and not on distinct grid points. I can't use raycasting, since they all move at the same time, so when there is a empty tile between two objects and they both want to go there, raycasting would allow it. I have tried using an array to set specific tiles invalid when occupied, but that somehow doesn't work during runtime (probably because I coded it wrongly) and suffers the same problem as raycasting anyway.

Anyone here has an idea, or am I screwed?

Comment
Add comment · Show 3
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 syclamoth · Sep 24, 2011 at 10:55 PM 1
Share

Well, you could do it in a two-pass way- every object sets the tile it wants to move to as 'occupied', and then the engine checks for whether any given object is allowed to move, given the one-step-forward occupation status! Just out of interest, if two objects want to move to the same grid location, how does it deter$$anonymous$$e which one gets to go first? I don't quite understand how this works, given that they 'all move at the same time'.

avatar image Piflik · Sep 24, 2011 at 11:07 PM 0
Share

Although they move at the same time, I doubt the engine calculates it at the same time for both objects, so whoever is calculated first, has priority, with the player having priority over all enemies.

How would I do this? Store all destinations in an array and then compare each to every subsequent one and recalculate (or stop) the ones that are the same? Wouldn't that take quite some time to calculate?

avatar image timberland8989 · Sep 25, 2011 at 03:32 AM 0
Share

Thanks for co$$anonymous$$g our timberland online store! Just enjoy yourself here! As a developping company, timberland boots always can give us some surprise .I belive timberland shoes Sale can make your feet more comfortable, make your life more stylish!If you want to have a try ,just click here: discount timberland boots Free delivery

timberland

timberland boot

timberland shops

timberland 2011

discount timberland

wholesale timberland

timberland chaussures

1 Reply

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

Answer by Eric5h5 · Sep 25, 2011 at 12:30 AM

Don't bother with physics and raycasting and so on, just use a 2D array that represents the game world. Check the array for what it contains, and do collision based on that.

Comment
Add comment · Show 7 · 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 Piflik · Sep 25, 2011 at 11:20 AM 0
Share

I got it. I guess this answer is quite close to what I did, although I had the idea yesterday before I went to bed, after reading syclamoth's comment and thinking some more, so I'll accept it...

$$anonymous$$aybe it helps others, so I'll write the complete solution here:

I first created a unique ID for every actor in the scene, by creating a static variable in a global script, writing that into the uniqueId variable in the enemies' script and increasing the static variable by one (function Start()). The Player has ID 0.

Secondly I created a static array in the same global script and store the grid based position (not the XYZ position) in that array at posArray[uniqueId]. The destination is tested against this array in a for loop and if the same grid point is already in there, the actor doesn't move. Then posArray[uniqueId] is overwritten with the new location.

On death posArray[uniqueId] is freed up again.

Thanks to both of you.

avatar image Eric5h5 · Sep 25, 2011 at 11:42 AM 1
Share

I would highly recommend using a 2D array like I mentioned ins$$anonymous$$d, that way you can directly look up whether a cell is occupied using the grid x/y coords in a single statement, rather than using a for loop.

avatar image Piflik · Sep 25, 2011 at 11:55 AM 0
Share

I tried that earlier, it was the first ting I tried, and multiple times, but it never worked. I had a 2D array that I filled with boolean values and if a tile was occupied I set the corresponding entry to false, but it was extremely slow during runtime and also not very reliable (probably because I did it wrongly).

avatar image Eric5h5 · Sep 25, 2011 at 12:04 PM 1
Share

There's no reason for it to be slow or unreliable, so I'd recommend fixing that code. It would be much faster than using a for loop.

avatar image Piflik · Sep 25, 2011 at 12:31 PM 0
Share

I guess the problem was that two or more objects wanted to write in the same entry at the same time, or some such, but I can try again...

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Adding collision detection to movement script... 1 Answer

2D Grid Movement (turn based) 2 Answers

Issues with manual collision detection involving updates... 1 Answer

A node in a childnode? 1 Answer

Collision detection in .fbx 0 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