- Home /
Rotate an instantiated GameObject towards another GameObjects pivot point
How can I rotate an instantiated GameObject towards another GameObjects pivot point when it is instantiated?
function checkForBuilding() { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100) && hit.collider.gameObject.CompareTag("Ground")){ //Tell unity there there is a building buildingHit = true; Debug.Log("Hit One"); myGround = (Instantiate(buildingObject,hit.point,transform.rotation)).transform; transform.LookAt(myGround); //Instantiate(Quaternion.identity); } yield(2);
}
My problem seems to be the object instantiates but doesn't rotate in time to face the target. I there a way for me to rotate the object before it even instantiates?
Thanks - C
Answer by Molix · Feb 16, 2011 at 02:36 PM
The following is untested, but it should be something like:
void SpawnPrefabAndRotate( Transform prefab, Transform target )
{
GameObject go = Instantiate( prefab.gameObject ) as GameObject;
go.transform.LookAt( target );
}
I don't really know JS, but I'm thinking it'll be something like:
function SpawnPrefabAndRotate( var prefab : Transform, var target : Transform )
{
var go : GameObject = Instantiate( prefab.gameObject );
go.transform.LookAt( target );
}
Sorry in Javascript please, I'm trying to incorporate it into my own javascript. thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613104725im_/https://answers.unity.com/themes/thub/images/avi.jpg)