I am placing a number of planks/boards onto each other.
I place them using a raycast, and get them to stick to the surface the raycast hits.
The ghost prefab (a simple Unity box) used has its localTransform adjusted to align with the surface that the cross-hairs are pointed at.
It works fine as long as the what I am placing onto is rotated 0 or 90 degrees so that my if’s catch the normals.
Can anyone help me with better code for the rotations.
The following code is inside a raycast:
//sizeX,sizeY,sizeZ are the renderer dimensions for the object placed.
ghostMaterials[ selectedMaterial ].transform.position = hit.point;
ghostMaterials[ selectedMaterial ].transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.normal == Vector3(1.0,0,0) ){
ghostMaterials[ selectedMaterial ].transform.localPosition.x += (sizeX / 2 ) ;
} else if( hit.normal == Vector3(-1.0,0,0) ) {
ghostMaterials[ selectedMaterial ].transform.localPosition.x -= (sizeX / 2 ) ;
} else if( hit.normal == Vector3(0,0,1.0) ) {
ghostMaterials[ selectedMaterial ].transform.localPosition.z += (sizeZ / 2 ) ;
} else if( hit.normal == Vector3(0,0,-1.0) ) {
ghostMaterials[ selectedMaterial ].transform.localPosition.z -= (sizeZ / 2 ) ;
} else if( hit.normal == Vector3(0,-1.0,0) ) {
ghostMaterials[ selectedMaterial ].transform.localPosition.y -= (sizeY / 2 ) ;
} else if( hit.normal == Vector3(0,1.0,0) ) {
ghostMaterials[ selectedMaterial ].transform.localPosition.y += (sizeY / 2 ) ;
}