Search Unity

[Isometric] Moving Objects problem

Discussion in 'Scripting' started by Gorki12, Sep 13, 2019.

  1. Gorki12

    Gorki12

    Joined:
    Jul 12, 2015
    Posts:
    73
    Hi, I have script where my object move in isometric view, but something work bad because on axis Y my sprites have bad position and I must manual add + 0.3f (screen 1).
    I want my sprites to have the correct positions like in screen 2.

    Addition question, how durring move object set my mouse position in place "X" on screen 1 ? (I think its correct mouse position to move objects in isometric view)

    Code:

    Code (CSharp):
    1. public class MoveObject: MonoBehaviour
    2. {
    3.     public float tileWidth;
    4.     public float tileHeigh;
    5.  
    6.     public float x;
    7.     public float y;
    8.  
    9.  
    10.     public Vector3 position;
    11.     public Vector2 buildingPosition;
    12.  
    13.     void Start()
    14.     {
    15.         // Get sprite size
    16.         tileWidth = (float)gameObject.GetComponent<Renderer>().bounds.size.x;
    17.         tileHeigh = (float)gameObject.GetComponent<Renderer>().bounds.size.y;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         x = (position.y * tileWidth / 2) + (position.x * tileWidth / 2);
    23.         y = (position.x * tileHeigh / 2) - (position.y * tileHeigh / 2);
    24.  
    25.         buildingPosition.x = x;
    26.         buildingPosition.y = y;
    27.  
    28.         transform.position = buildingPosition;
    29.  
    30.     }
    31. }
    "position" I set manual, but if I set [1,0, 0] code calculate to correct isometric position but not for axis Y :( .
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I have never tried using the bounds size stuff like that, so I'm not sure it is reliable for this sort of alignment thing.

    Is it possible your sprites have extra space around them, maybe at the top or the bottom? What about the pivot spot on your sprites: is the pivot in a reasonable location and consistent across all sprites?

    Another thing is to simply use the vertical size of your sprite in pixels, divided by the import setting "Pixels Per Unit" to set your positions.
     
  3. Gorki12

    Gorki12

    Joined:
    Jul 12, 2015
    Posts:
    73
    I tryed change pivot but this change only position all sprites this type so still there's been an interval between sprites.
    Pixels Per Unit change size.