Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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?