Object rotation with out hit.normals (the correct way question)

I have a method that do a raycast to a wall, and return the out hit.normal. Then, apply to object so it can rotate in the right position.

The problem I was facing before this workaround is that to get my rotation right, I needed to manually rotate the object 90 degrees on the x axis. To prevent this, I come with this manual change on the out hit.normal.

I think is badly implemented, but works. After the raycast, took out the out hit to Vector3 myhit

            normal = myhit.normal;

            //flipping the normals

            float x = normal.x;
            float y = normal.y;
            float z = normal.z;
           
            normal.x = x;
            normal.y = -z; //needed to flip
            normal.z = y;  //needed to change

Object code for rotate

public GameObject tempghost;
tempghost.transform.rotation = Quaternion.LookRotation(normal);

Its working, but I feel like its wrong, and its possible to generate errors in the future…

LookRtoation has another overload where you can supply 2 directions, (forward and up) instead of only 1 (which is treated as forward only).
Sounds like you want the hit normal to be the up direction, so you need to use the overload

1 Like

Thank you. Got it working with:

        tempghost.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal);