Rotate Instantiated GameObject

I’m having some trouble figuring out how to rotate an instantiated object onto the face of an object it was instantiated on, not using raycasts.

Currently, two objects colliding instantiate another object which is in its default prefabed position, this is how it’s intended to be on horizontal surfaces however how can I rotate the objects when they hit vertical faces?

You may generate a rotation to align any face of your object to some vector using Quternion.FromToRotation(from, to) - it creates a rotation that turns the vector from to the same direction as the vector to.

But there are some shortcuts that may be much easier to use: you can assign the direction vector to the object’s transform.forward (or transform.up or transform.right) to make the desired side look in that direction. Supposing you want to align the new object’s (newObj) forward direction to an object objA, for instance, you can use this:

  // create the direction vector newObj->objA
  var dir: Vector3 = objA.transform.position - newObj.transform.position;
  // make the newObj forward face look to objA:
  newObj.forward = dir;
  // or make the newObj up face to look to it:
  newObj.up = dir;
  // or even make its bottom side look to objA (sounds weird...):
  newObj.up = -dir;