Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Prevent movement off of level.

Discussion in 'Scripting' started by purplexis, Oct 11, 2006.

  1. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    I am working in a situation where a character is moving around a board-game like level using a simple transform.translate - however, I don't want the character to be able to move off of the edge of the level.

    I want to do this by not allowing the character to never not be in contact with the collider of the level. Is there any good way for me to do this?

    Or is there a way I can enact bounds to simply prevent the user from moving outside the level?
     
  2. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    I'm trying to set up a 'bounds' var and use its contains function in an if statement. Hopefully it will work.
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Or just add invisible "wall" colliders. An object without a renderer can still block objects physically.

    My game has an "invisible fence" like that--a ring-shaped mesh collider with polys facing in, but not rendered.
     
  4. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    I've tried that. Doesn't seem to want to work. I think it's because I'm using transform.translate to move - so I effectively 'teleport' past the collider.
     
  5. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Ah--then making sure the translate won't happen if it puts you outside the bounds makes the most sense to me. (At least, if the level is rectangular.)
     
  6. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Luckily it is rectangular. I would be rather unhappy if it wasn't.
     
  7. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Pretty sure this is what the CharacterController is for - to have simple movement that takes into account walls and collisions for free.

    Why not upgrade to it?

    -Jon
     
  8. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    I am using Character Controller..

    My issue is that I am essentially emulating a board game. The character moves a fixed distance from tile to tile. The way I found to move most easily was transform.translate. Is there a better way for me to move a fixed distance?
     
  9. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Here's a sample of my code:

    Code (csharp):
    1.  
    2.     if (Input.GetKeyDown ("down")){
    3.  
    4.            
    5.         if (bounds.Contains(controller.center + new Vector3(0,0,-speed))){
    6.             transform.Translate(Vector3(0, 0, -speed), controller.transform);}
    7.  
    8.     }
    9.  
    'speed' is set to the fixed distance I move.
     
  10. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    What behavior is that script giving you? What does bounds represent in this situation since it does not specifically reference an object? The bounds of the Mesh, the Renderer, or the Collider? It seems like you would want to get the bounds of the level by having a variable to hold a reference for the level and get the bounds from that.
     
  11. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    I have no idea what you just said.

    I barely understand what bounds does - and I'm not surprised that its not working the way I had hoped.

    Any suggestions from individuals far better versed in the wonders of Unity than I?