Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Dont let player enter certain zones

Discussion in 'Editor & General Support' started by dirfinyu, May 2, 2020.

  1. dirfinyu

    dirfinyu

    Joined:
    Sep 6, 2012
    Posts:
    13
    Hi. I have a complex world and have a terrain, terrains y position is 500, and have a plane under it at position 499.9 that represents water.
    I move player this way:
    Code (CSharp):
    1. Vector3 mov = new Vector3(-5, 0, 0) * Time.deltaTime;
    2.                 MyRigidbody.MovePosition(transform.position + mov);
    I need Player cant enter to plane at position 499.9, until he have certains conditions. Like it collide with a box collider. I cant put box collider over that plane, and when player enters mark that collider at tag, its not posible.
    Here you have the 2 zones players have. Zone A always can move, and zone B only can move with certains conditions.

    What is the best way to do it?
    Thank you and sorry for my english!
     
  2. Jenifer_Jane

    Jenifer_Jane

    Joined:
    Jan 24, 2017
    Posts:
    69
    mathf.clamp or Colliders for boundary then disable with code as per your need
     
  3. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    659
    If the plane has collision, you can detect collisions between the plane and the player - if there is one, move him "away" - "away" is not a vector xD so you could try to add "markers" - basically empty gameobjects (or just a bunch of stored Vector3 points) on the water, then you can move the player "away" from the point - which is a vector.
    So
    mov = (transform.position - marker).normalized * 5;


    Finding the closest marker is extremly easy - just connect the markers to the parts of your water (even if the water itself is just one huge plane, the colliders of said plane can be seperated - so you don't have to check all the markers in the game once the player collides with the water, but just the ones of one area) either in Hierarchy or via Code.
     
    dirfinyu likes this.