• 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 Ozale · Feb 19, 2011 at 02:01 PM · collisioncontrolsmaze

Maze-game control question

G'day guys, I'm working on a small project - a 'maze game', akin in movement to Pacman. I've built a player movement system with three values - 'desiredDirection', 'availableDirection' and 'actualDirection'. You can guess how these are used easily enough - the player can set their next desired direction change, and as soon as they reach a junction where that direction is available, they change course.

I've got the player moving anyway they wish (North, South, East or West), but 'availableDirection' hasn't been implemented yet. The way I see it i have three techniques available to me, and I'm seeking advice on which is best:

1) Scripted junctions = Write script that basically creates the junction points through pure code, ie "if player position = junction position, available directions are etc."

2) Physical junctions = Collisions with invisible boxes - 'on collision, available directions are..."

3) Raycasting = Saw this in a post on answers. Feels pretty risky to me, but it could be great, as it'd be dynamic and wouldn't need adjusting per 'maze'.

If you guys could help steer me in the right direction, I'd be most appreciative. I've tried getting the collision technique to work, but for some reason i just can't get a cube to react to a collision with my player object.

And no I'm not looking to build Pacman. :P

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

3 Replies

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

Answer by Eric5h5 · Feb 19, 2011 at 03:57 PM

If the game really is like Pacman, then the maze is on a grid, in which case the fastest/easiest thing is to have a 2D array that represents the maze, and you get available directions from the array. For example, Realmaze3D has no colliders, raycasting, or anything like that, just a 3D array of integers.

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
avatar image
1
Best Answer

Answer by GesterX · Feb 19, 2011 at 02:55 PM

I would go with RayCasting.

Check out the scripting reference here:

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

For example this:

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 2.0)) {
print ("There is an object in front of the player");
}
}

This will detect if there is an object 2 metres in front of the player and print the line "There is an object in front of the player" to the debug console.

You could have 4 raycasts. The one above for forward, and 3 more for your other directions (just change the transform.forward part of the raycast to cast the ray in the other directions).

Finally for debugging you can use the DrawRay Class to actually visualise the RayCast:

http://unity3d.com/support/documentation/ScriptReference/Debug.DrawRay.html

I haven't given you the exact script you need here, just a few nods in the right direction. It''s easier to learn this way :-)

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
avatar image
0
Best Answer

Answer by Ozale · Mar 08, 2011 at 10:09 PM

Sorry for closing this so late, but I've since built and completed a working and efficient system. I could have created the X/Z array, but in this instance I've created 'if' statements detecting the player location (if transform.position.x == 0 && transform.position.z == 3) etc. instead. I suppose the reason I didn't go with the array was multimensional array support seems a bit weird in Unity.

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 Eric5h5 · Mar 08, 2011 at 10:23 PM 0
Share

Using a ton of if statements isn't efficient, and hard-coding everything like that is extremely inflexible. $$anonymous$$aybe you could say what problems you're having with arrays, since I can't think of anything offhand that's "weird".

avatar image Ozale · Mar 10, 2011 at 08:07 AM 0
Share

It was mostly that the little mention of multidimensional arrays in Unity was that they weren't supported previously (at least in JS). You're right about efficiency, a 10x10 map runs fine as an executable, but I'm certain it would be terrible mounted in a browser or mobile device. I don't really know where to begin converting my movement system to use arrays. I'd have to show you an example of the code.

avatar image Ozale · Mar 10, 2011 at 09:35 AM 0
Share

I sent you my email address via your site's contact form

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

is there a way to move a rigidbody with something like moveposition without clipping through collider? 1 Answer

Generating 2D collisions for maze 0 Answers

Character doesen't collide 3 Answers

Rigid Body Collision Detection 1 Answer

FPController see through walls problem 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges