- Home /
How do i get the Raycast rotation?
So i am making an Island with trees with raycasting, and i would like to know how can i instantiate an object(tree) at a raycast point and raycast rotation like in Pic1. Would anyone know how to do this?
-Pic1
http://i.imgur.com/FEGWBN8.png
(I could not upload the picture for some reason :|)
So do you want the normal of the surface hit, or the direction in which the ray was traveling?
@Namey5 I want the direction of the ray and have its rotation to be instantiated with the cube. http://i.imgur.com/HByZV$$anonymous$$L.png
Answer by villevli · Sep 20, 2016 at 06:43 PM
Make sure the pivot of the tree object is at the bottom of the object. (the point where it should connect to the surface) If you can't modify it in a 3D modeling application you can do it in Unity by creating a new empty gameobject and placing a gameobject with the mesh as a child and modifying the child's position.
Then in your Raycast script:
tree.transform.position = hit.point;
tree.transform.up = hit.normal;
(if you did the pivot trick, tree is a reference to the parent object you created)
If you want to use the direction of the ray and not the normal of the surface then just set transform.up to that direction vector (which you should already have when creating the raycast). You probably have to multiply it by -1 so it points to where the ray came from.
@villevli And what do i do when i have to instantiate the trees?