Search Unity

Placing GameObjects next to each other

Discussion in 'Getting Started' started by Terkin, Mar 23, 2015.

  1. Terkin

    Terkin

    Joined:
    Mar 23, 2015
    Posts:
    1
    Hi, I want to place a GameObject next to another one but the position should depent if the hit.point is more to left, right, top or buttom. I came up with this

    public static Vector3 relativDiagonalPosition(Vector3 hit,Transform obj){
    float x = 0f;
    float z = 0f;
    if(hit.x < obj.position.x){
    x = 1f;
    } else {
    x = -1f;
    }

    if(hit.z < obj.position.z){
    z = 1f;
    } else {
    z=-1f;
    }

    return new Vector3(x,0f,z);
    }​

    But this does not respect the angle of the GameObject. I think I read of a method to transform a vector relativ to another vector but i can't remember which and does not find it without a propper keyword ...
    By the way how do I post code correct?