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. Dismiss Notice

The resultant of 3 normals

Discussion in 'Scripting' started by MikeyJY, Jun 24, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    529
    Yesterday I created an thread about Terrain normal. I figured it with the help of users which post in the thread and I'm very proud of the result:
    In the video the cyan arrow is the normal of terrain, and I snapped the object on terrain sample height and it fits the terrain. When I place on the flat surface the angle between y-axis and normal is 0, so I know it is working:


    However, I want to add the feature to player to rotate the building around the y axis, but when I do this:

    (I calculate all in OnGizmosDraw to test in editmode - i'll move in LateUpdate)

    The terrain fit breaks because the normal is calculated at the pivot and doesn't take care of y rotation so it works only when it is rotated default:


    I think that I need to calculate 3 normal vectors at the extreme points of the object and somehow to compose the 3 vectors and find the resultant of them


    [If a moderator sees this sorry if I posted so many threads in last week, but I'm very motivated to finish my project, and maybe I choose a project to ambitious for my limited knowledge, but I'm about to finish and I can't give up]
     
    Last edited: Jun 24, 2020
  2. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    529
    I have the solution. It is so simple:
    Code (CSharp):
    1. RaycastHit hit;
    2.             if (Physics.Raycast(Blueprint.transform.position, Vector3.down, out hit))
    3.             {
    4.  
    5.                 Blueprint.transform.rotation = Quaternion.FromToRotation(Blueprint.transform.up, hit.normal) * Blueprint.transform.rotation;
    6.             }
    It is a little bit glitchy, but still amazing: