- Home /
Question by
PixelJam Studios · May 24, 2015 at 07:16 PM ·
prefabtriggerrotate
Building system - Spawning prefab on trigger with raycast and rotating it.
I would like to know if it is possible to spawn in a prefab on the exact position of a trigger collider and rotate it to that colliders rotation.
#pragma strict
var buildFoundation : boolean;
var buildPilar : boolean;
var buildWall : boolean;
var buildRoof : boolean;
var Foundation:GameObject; //assign in Object inspector
var Pilar:GameObject;
var Wall:GameObject;
var Roof:GameObject;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var fwd = transform.TransformDirection (Vector3.forward);
Inventory ();
if (Physics.Raycast (transform.position, fwd, hit, 10)) {
if(hit.collider.tag == "Foundation" && Input.GetMouseButtonDown(0) && buildFoundation == true) //name the tag as you wish, unsure if it is //hit.collider.gameObject.tag
Instantiate(Foundation, hit.transform.position, Quaternion.Euler(0,0,0));
Debug.DrawLine (ray.origin, hit.point);
}
if(hit.collider.tag == "Pilar" && Input.GetMouseButtonDown(0) && buildPilar == true) { //name the tag as you wish, unsure if it is //hit.collider.gameObject.tag
Instantiate(Pilar, hit.transform.position, Quaternion.Euler(0,0,0));
Debug.DrawLine (ray.origin, hit.point);
}
if(hit.collider.tag == "Wall" && Input.GetMouseButtonDown(0) && buildWall == true) { //name the tag as you wish, unsure if it is //hit.collider.gameObject.tag
Instantiate(Wall, hit.transform.position, Quaternion.Euler(0,0,0));
Debug.DrawLine (ray.origin, hit.point);
}
if(hit.collider.tag == "Floor" && Input.GetMouseButtonDown(0) && buildRoof == true) { //name the tag as you wish, unsure if it is //hit.collider.gameObject.tag
Instantiate(Roof, hit.transform.position, Quaternion.Euler(0,0,0));
Debug.DrawLine (ray.origin, hit.point);
}
}
function Inventory () {
if(Input.GetKey(KeyCode.Tab)) {
// INVENTORY GUI CODE
}
if(Input.GetKey(KeyCode.Alpha1)) {
buildPilar = false;
buildFoundation = true;
}
if(Input.GetKey(KeyCode.Alpha2)) {
buildPilar = true;
buildRoof = false;
buildFoundation = false;
}
if(Input.GetKey(KeyCode.Alpha3)) {
buildWall = true;
buildRoof = false;
buildPilar = false;
buildFoundation = false;
}
if(Input.GetKey(KeyCode.Alpha4)) {
buildRoof = true;
buildWall = false;
buildPilar = false;
buildFoundation = false;
}
}
Thanks for reading.
Comment