I would like to instantiate an object with the correct rotation but unsure about how I’d do it. If i Instantiate the object on a flat surface it looks fine but if I Instantiate on a vertical or sloped wall, it looks weird. Take the below image as an example:
[54241-capture.png*_|54241]
The slime on the right is flat on the ground but the one on the left is in the same rotation but sticking from the wall. Is there a way I can get the object to like against the wall for a more natural look? This is the code I’m currently using:
#EDIT
I found a post [here][2] that says I should rotate according to the normals of the object which makes sense but I am still having no luck using the following code:
RaycastHit hit;
Vector3 dir = -transform.up;
float length = 0.35f;
Debug.DrawRay(transform.position, dir * length, Color.green);
if (Physics.Raycast(transform.position, dir * length, out hit, length))
{
SpawnObject(null, manager.slimeBalls, transform.position, Quaternion.LookRotation(hit.normal));
}
Just for reference, this is the SpawnObject code I am using:
void SpawnObject(Collider col, List<GameObject> objectList, Vector3 position, Quaternion rotation)
{
//For each artifact in the list
for (int i = 0; i < objectList.Count; i++)
{
//If a artifact is inactive
if (!objectList*.activeInHierarchy)*
{
//Set the position to the collision point
objectList*.transform.position = position;*
//Set the rotation to the rotation collision point
objectList*.transform.rotation = rotation;*
//Set the artifact to active
objectList*.SetActive(true);*
//break from the loop
break;
}
}
}
*
*[2]: http://answers.unity3d.com/questions/16952/instantiate-based-on-raycasthitnormal.html*_