hey, i am currently working on a little treehouse builder and got some problems with aligning the wooden planks to the tree normals.
to get the normals, i do a very simple raycast - i dont think there is anything wrong with it, just for the record:
if (Physics.Raycast(from, dir, out hit, Mathf.Infinity, _treeLayer))
{
hitpoint = hit.point;
hitnormal = hit.normal;
hitobject = hit.transform.gameObject;
Debug.DrawRay(from, dir, Color.red);
}
and to rotate the blue object to the nomrals, i do this:
var newRot = Quaternion.LookRotation(Vector3.ProjectOnPlane(Vector3.up, hitnormal), hitnormal);
_objectToPlaceRoot.transform.rotation = Quaternion.Slerp(_objectToPlaceRoot.transform.rotation, newRot, RotationSpeed * Time.deltaTime);
this is how it looks like:
the problem is, that the Y rotation of the blue plank, should remain like how it was on the main tree, when it goes over to the horizontal tree part, it flips, that should not happen. any idea how?
thanks!
edit. i tried it with
var newRot = Quaternion.FromToRotation(_objectToPlaceRoot.transform.up, hit.normal) * _objectToPlaceRoot.transform.rotation;
_objectToPlaceRoot.transform.rotation = Quaternion.Slerp(_objectToPlaceRoot.transform.rotation, newRot, RotationSpeed * Time.deltaTime);
and its better. i dont get the same angle when i switch to another tree branch but… its getting there