Search Unity

How detect player is which side of box?

Discussion in 'Scripting' started by AMIR_REZAs, May 22, 2015.

  1. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    I have a 3d box like this picture.
    An a player in 3d.
    Now how can I detect which side of box my player is? 1 or 2.

    I've tested calculating angles between box's transform.right and player.transform.forward but it doesn't work nice. What should I do ?

     
  2. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    i cannot see ant picture, can you upload again?
     
  3. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    343
    You could have two new box colliders for each side. Then you could create function OnTriggerEnter on your player script.
     
    Lethn likes this.
  4. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    yes, you can create 2 empty gameObject and add colliders.
    Then use trigger function to detect tag / name.
     
  5. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    yes, here u are


    Thanks a lot, It's a way but I want to use one collider.
     
  6. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    How about Vector maths?
    dif = Player pos.y - ground pos.y
    if(dif > 0), = 2
    else if (dif < 0) = 1
    else return null
     
  7. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Code (CSharp):
    1. void OnTriggerEnter(Collider trigger){
    2.     if(trigger.gameobject.tag == "upper"){
    3.         //Do somthing if upper
    4.     }
    5.  
    6.     if(trigger.gameobject.tag == "lower"){
    7.         //Do somthing if lower
    8.  
    9.     }
    10. }
    Make sure you tick the "Is Trigger" in the box collider's inspector.

    EDIT: My bad, just seen the only one collider message


     
  8. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    What you're really asking is to test which side of a plane (your box wall) a point (game object position) is on. A plane is defined by a normal (the direction it's pointing) and a location. To check which side of it a point is one, you take the direction from the origin of the plane to the point. If the angle between the direction and your plane's normal is greater than 90 degrees, is on the backside of the plane.



    Taking the origin of your plane to be the center of your box...

    Code (csharp):
    1. Vector3 direction = playerPosition - planeOrigin;
    2. if (Vector3.Angle(direction, planeNormal) > 90)
    3. {
    4. // do something
    5. }
    This is a good way of testing this since it works in all directions and all dimensions.
     
  9. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    Thank you all, solved.


    Great idea, it works nice....
     
  10. utamas2003

    utamas2003

    Joined:
    Oct 20, 2018
    Posts:
    2
    Hey!
    I would love to do what you wrote, but i can't understand plane normal. Can you help me what does it mean and how it's works? :D
     
  11. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    He explains it with the image? There are some helper methods etc you can use too. Create a plane

    Code (CSharp):
    1. var plane = new Plane(normal, point);
    2. var onPositiveSide =  plane.GetSide(viewerPosition);
    edit: You need to understand these basics to create a 3D spaced game
     
    utamas2003 likes this.