- Home /
Instantiating an object with the correct rotation
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:
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 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[i].activeInHierarchy)
{
//Set the position to the collision point
objectList[i].transform.position = position;
//Set the rotation to the rotation collision point
objectList[i].transform.rotation = rotation;
//Set the artifact to active
objectList[i].SetActive(true);
//break from the loop
break;
}
}
}
You could use a Raycast downwards of the splash and use hit.normal for it's rotation.
Something like :
Ray ray = new Ray (transform.position, Vector3.down);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 10)) {
Debug.Log("Hit Point : " + hit);
Vector3 hitPoint = hit.point;
Quaternion vnorm = new Quaternion(hit.normal.z, hit.normal.y, -hit.normal.x, 1);
Instantiate (splash, hitPoint, vnorm);
}
I'm not sure if it will work for you, but it worth a try.
@Positive7 thanks for the response but no luck with that unfortunately, I've added a bit more to my question
Answer by Eno-Khaon · Sep 13, 2015 at 07:14 PM
At the moment, you're telling your object's "forward" direction to orient itself facing outward from the surface it's sitting on.
To get the rotation you're trying for, you need to add a small, extra step to your process.
Using FromToRotation(), you can adapt your rotation into a small tweak rather than a large change in direction.
Quaternion newDirection = Quaternion.FromToRotation(transform.up, hit.normal);
SpawnObject(null, manager.slimeBalls, transform.position, newDirection);
On this basis, the newDirection would be attained as a relative rotation to the spawner. As an alternative, "Vector3.up" could be used as an alternative to "transform.up" in case the spawner would be rotated.
@$$anonymous$$o $$anonymous$$haon I don't know why but I can't this to work at all, nothing is happening with the new code unfortunately, am I missing something to this?
void Update()
{
RaycastHit hit;
Vector3 dir = -transform.up;
float length = 0.35f;
if (Physics.Raycast(transform.position, dir, out hit, length) && !running)
{
running = true;
Quaternion newRotation = Quaternion.FromToRotation(transform.up, hit.normal);
SpawnObject(null, manager.slimeBalls, transform.position, newRotation);
running = false;
gameObject.SetActive(false);
}
}
I got it in the end, the Animator on the object seemed to be freezing its rotation, i disabled the animator and everything works fine now, thanks for the answer :)
Your answer
