Search Unity

How to calculate the correct possition?

Discussion in 'Scripting' started by computerfreak, Jan 26, 2015.

  1. computerfreak

    computerfreak

    Joined:
    Dec 28, 2014
    Posts:
    15
    I have two positioning problems and I don't know how to do it correctly:

    1. I have a canvas which has the render mode screen space. This canvas has a child canvas which contains my menue. Now I would like to position this child canvas in the center of the main canvas. I don't succeed with that. I tried to place the canvas in the inspector and used these x,y, and z values in the script then but my canvas was placed somewhere else instead. I guess the reason is the screen space mode. How can I find the correct position for my canvas in this render mode?


    2. My terrain is not plane, it has hills and such things. Now I want to place a game object on the ground with a C# script. How can I find out which is the correct height and rotation on the place where I want to place my object? I tried this but it doesn't place the correct position
    Code (csharp):
    1.  
    2. Vector3 position = instance.transform.position;
    3.  
    4. float y = Terrain.activeTerrain.SampleHeight(position);
    5. float x =instance.transform.position.x;
    6. float z =instance.transform.position.z;
    7.  
    8. instance.transform.position = new Vector3(x, y, z);
    9.  
     
  2. tobyheadcast

    tobyheadcast

    Joined:
    Aug 5, 2014
    Posts:
    25
    For the terrain you could raycast down onto the terrain from above and get the hit location, from that you will get a position and normal you can use to place your object on the terrain correctly.
     
  3. computerfreak

    computerfreak

    Joined:
    Dec 28, 2014
    Posts:
    15
    thank you I'm going to use that