Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

RTS Grid/Tilemap system

Discussion in 'Scripting' started by unity_U29e6nvxBv4W1g, Oct 13, 2018.

  1. unity_U29e6nvxBv4W1g

    unity_U29e6nvxBv4W1g

    Joined:
    Sep 5, 2018
    Posts:
    3
    Hello everyone , so i am trying to create a grid system for building placement and the problem i have is what i dont know how to calculate y axis on inclined surface. So what am i doing :
    1: Sending a raycast
    2: Rotating placeable object depending on hit.normal
    3: Sending the raycast.hit.point and hit.normal to my method
    4: Checking if normal angle not equal to null , if it is equall y = hit.point.y, if it is not =>7
    5: Getting x and z axises by dividing them on grid cell size , rounding them to nearest integer and multiply them on grid cell size
    6: Storing x and z values to oldx/oldy
    7: If old x not equal to new x (so that means what object moves) and angle is not equal to 0 (that means we are on inclined surface) then we are calculating height of triangle with same angle , but foundation's size equals to our grid cell size and each time x value changes , we are multiplying height to value change divided by size and send this to y value.
    8: Sending back vector3 pos

    The main problem now is what for some reason height is bigger , than it suppose to be.
    I think i am just missing something in calculations, please help me find what am i doing wrong.
    Also if you know another way of y axis calculation , feel free to say .
    So here is my code:
    Code (CSharp):
    1.  public Vector3 GetNearestPointOnGrid(Vector3 position, Quaternion quat)
    2.     {
    3.    
    4.    
    5.         float angle = quat.z * 100 ;
    6.    
    7.         int xCount = Mathf.RoundToInt(position.x / size);
    8.         int zCount = Mathf.RoundToInt(position.z / size);
    9.    
    10.         float height;
    11.         if (oldxCount != xCount)
    12.         {
    13.             if (angle != 0)
    14.             {
    15.                 height = (size / Mathf.Cos(angle)) * Mathf.Sin(angle); // height = hypotenuse * sin(angle) , hypotenuse = foundation * cos(angle)
    16.            
    17.                 print(angle);        
    18.                 yCount += height * ((xCount - oldxCount) / size);
    19.            
    20.             }
    21.             else
    22.             {
    23.                 yCount = position.y;
    24.  
    25.             }
    26.         }
    27.         else { yCount = oldyCount; }
    28.    
    29.  
    30.         Vector3 result = new Vector3(
    31.             (float)xCount * size,
    32.             (float)yCount ,
    33.             (float)zCount * size);
    34.    
    35.         oldxCount = xCount;
    36.         oldyCount = yCount;
    37.         oldzCount = zCount;
    38.         return result;
    39.  
    40.  
    41.     }
    Screenshot_76.png
    P.S. For now , even with right height value it will works only for z rotation and on the surface deviated relative to the abscissa x. And sorry for my poor english)
     
    Last edited: Oct 14, 2018
  2. unity_U29e6nvxBv4W1g

    unity_U29e6nvxBv4W1g

    Joined:
    Sep 5, 2018
    Posts:
    3
    Alright , i solved it by myself . If anyone want to know , Mathf.Sin(float f) use radians , when in my method i tried to paste angle in degrees . If you simply change "height = (size / Mathf.Cos(angle)) * Mathf.Sin(angle); " to " height = Mathf.Tan(angle * Mathf.PI / 180) * size; " everything works fine.
     
  3. unity_U29e6nvxBv4W1g

    unity_U29e6nvxBv4W1g

    Joined:
    Sep 5, 2018
    Posts:
    3
    Also in arguments change quatornion quat to float f or in the method instead of float " angle = quat.z * 100 ; " use "angle = quat.eulerAngles.z;"