Search Unity

[Solved]The position of objects in space.

Discussion in 'Scripting' started by mikhach, Dec 8, 2014.

  1. mikhach

    mikhach

    Joined:
    Dec 8, 2014
    Posts:
    5
    I am familiar Unity recently and am trying to create a game in the classic isometric view. And then the question arose as to realize the position of the character relative to objects (trees, walls, houses) that when it is in front of and behind the object. Can someone tell me the solution of this problem.
     
  2. mikhach

    mikhach

    Joined:
    Dec 8, 2014
    Posts:
    5
    I'm trying to like this:
    Code (CSharp):
    1. public class position : MonoBehaviour {
    2.     public GameObject player;
    3.     public GameObject[] trees;
    4.     // Use this for initialization
    5.     void Start () {
    6.                 player = GameObject.FindWithTag ("Player");
    7.                 trees = GameObject.FindGameObjectsWithTag ("Tree");
    8.         }
    9.     // Update is called once per frame
    10.     void Update () {
    11.         foreach (GameObject tree in trees) {
    12.                         if (player.transform.position.y > tree.transform.position.y) {
    13.                                 player.renderer.sortingOrder = -1;
    14.                         } else {
    15.                                 player.renderer.sortingOrder = 1;
    16.                         }
    17.                 }          
    18.     }
    19. }
    The does not work.
    Please, help me.
     
  3. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    You're changing player's sortingOrder every iteration thus overwriting last assignment each time your code iterates. What is it exactly you want to achieve with this?
     
  4. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    What about this?

    Code (CSharp):
    1.  if (player.transform.position.y > tree.transform.position.y) {
    2.                                 tree.renderer.sortingOrder = -1;
    3.                         } else {
    4.                                 tree.renderer.sortingOrder = 1;
    5.                         }
    6.                 }          
     
  5. mikhach

    mikhach

    Joined:
    Dec 8, 2014
    Posts:
    5
    Solved:
    Code (CSharp):
    1. renderer.sortingOrder = (int)(transform.position.y * -10);