Search Unity

Convert world to local position

Discussion in 'Scripting' started by zenden_1, May 27, 2019.

  1. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    Helo, i just want to concert my position but i have that :



    Normally, the template block have to bet just next to to parent.


    Code (CSharp):
    1. if (Physics.Raycast(ray, out hit, maxSetDistance))
    2.         {
    3.             Vector3 point = ray.GetPoint(hit.distance - 0.02f);
    4.             templateBlock.SetActive(true);
    5.  
    6.  
    7.             Vector3 _position = new Vector3(
    8.                 Mathf.Round(point.x / _blocKPosRound) * _blocKPosRound,
    9.                 Mathf.Round(point.y / _blocKPosRound) * _blocKPosRound,
    10.                 Mathf.Round(point.z / _blocKPosRound) * _blocKPosRound);
    11.  
    12.             if(hit.transform.parent != null)
    13.                 templateBlock.transform.parent = hit.transform.parent;
    14.             else
    15.                 templateBlock.transform.parent = hit.transform;
    16.  
    17.             Vector3 _LocalPosition = templateBlock.transform.parent.InverseTransformDirection(_position);
    18.  
    19.            
    20.             templateBlock.transform.localPosition = _LocalPosition;
     
  2. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    In hierarcy templateblock goes as a "templateblock parent" child?


    Code (CSharp):
    1. if(hit.transform.parent != null)
    2.                 templateBlock.transform.parent = hit.transform.parent;
    3.             else
    4.                 templateBlock.transform.parent = hit.transform;
    5.  
    this if i dont really get it.

    Code (CSharp):
    1. //Maybe you should delete the if and say
    2. templateBlock.transform.parent = hit.transform;
    3.  
    4.  
     
  3. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    I don't know if your modification will change something but I will Try that tomorrow.

    If somebody have another solution..