- Home /
Placing tree aligned to planet normal - wrong rotation?
Hi,
I'm trying to place trees aligned to normal of the planet, which I compute by making a raycast from mouse click point. I understand the math behind this, but still got some problems. The normal vector is correct (white line on screenshot), but trees are still placed in wrong orintation. Could someone look at my code and give some advice? Thanks in advance :-)
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit; ;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject obj = GameObject.Instantiate(tree, hit.point, Quaternion.LookRotation(tree.transform.forward, hit.normal)) as GameObject;
}
}
![alt text][1] [1]: /storage/temp/7176-capture.png
Answer by kulesz · Jan 26, 2013 at 01:54 PM
OK, actually it seems I've answered myself in the meantime. It works with:
GameObject obj = GameObject.Instantiate(tree, hit.point, Quaternion.identity) as GameObject;
obj.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
However I still don't know why previous method generated such strange results. I appreciate if someone could explain it to me :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220613091621im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Instantiate onto Planet 4 Answers
Checking if object intersects? 1 Answer
Cut a tree created with the tree creator ? 2 Answers
RaycastHit.normal and Orientation to a Path 1 Answer
need help with roation 2 Answers