I am wanting to add a collider to the scene and move it about with my CharacterController for a tile-based game. There are other possible solutions to this, but ideally, when the player moves to another tile, I would move another collider to that tile, effectively reserving the tile and preventing enemies from trying to enter the tile at the same time. I can't see a way that this would be possible, but I was wondering it it could be done with layers? Can a CharacterController be told to ignore a layer? In which case, I can put all these reservations on this other layer and reference that layer when making decisions about where players and enemies can go and this makes it quite easy and efficient (I hope!).
Thanks in advance Ian H
asked Nov 10, 2010 at 12:06 PM
I solved this by creating a prefab consisting of an empty gameobject with a BoxCollider attached. The box collider takes up a tile and the script controlling movement instantiates two of these prefabs. One always occupies the current 'tile' and the other is inactive until the character moves, at which point the 2nd prefab is activated and placed at the designation tile location. When the controller completes the movement from tile to tile, the first prefab is moved to the controller's new location and the 2nd is set inactive. It us necessary to prevent collision between the CharacterController and these two colliders, and because I use raycasting to determine whether a controller can move into the next square, I have to make the first collider inactive while raycasting.
Sorry if this is a bit hard to grasp from my answer - think of the Box
It seems to be fine and I think I will be able to adapt it to creatures of different size eventually.
answered Dec 02, 2010 at 04:55 PM
May I suggest you solve this program using a programmatical approach with a Grid that can reserve/flag positions as taken/not taken rather than physically blocking off a location? I believe that could help you get a lot more control over what is going on since every tile can be used as an object that way. It has a specific property or behaviour or anything you want keeping all the knowledge clear and open for yourself and any other programmer that will need to dig through the design.
If you really just want to go about it , I would suggest trying to create a temporary clone of your object and remove it's properties that make it visible or controllable in any other way than collisions. Anything that wishes to move simply has to do a range limited (Already a constructor for that exists) sphere or ray cast.
answered Nov 10, 2010 at 03:01 PM