- Home /
Instantiate rotation question.
var tempbrick : GameObject;
function Update () {
if (Input.GetButtonDown("Brickcast")) {
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 7)) {
Debug.DrawLine (ray.origin, hit.point);
Instantiate(tempbrick, hit.point, Quaternion.identity);
}
Physics.Raycast (ray, hit);
}
Debug.DrawRay (ray.origin, ray.direction * 10, Color.white);
}
I want to instantiate the prefab with the same rotation as the object that the raycast collided with. How?
Answer by Joshua · May 08, 2011 at 01:58 AM
var rotationOfHitObject : Quaternion = hit.collider.gameObject.transform.rotation;
Put this in after casting the ray to get the rotation of the object the ray hit. Then replace Quaternion.identity with rotationOfHitObject. Untested, but I'm confident this should work. Good luck with it!
I got "NullReferenceException: Object reference not set to an instance of an object" in the console ins$$anonymous$$d. Hm.
And you're putting it inside the if() {...} statement, so you're sure the ray is instersecting?
Right, but that's not good. Now you're trying to access the transform of the object your ray has hit before casting the ray. When I sayd put it inside the if function I meant inside the one where you cast the ray. Put it right above the Debug.DrawLine-line ;)
OH $$anonymous$$Y LORD YES. IT WOR$$anonymous$$S
YOU WIN EVERYTHING. THAN$$anonymous$$ YOU!