- Home /
Raycast hitting trigger to spawn in a prefab at the triggers location.
Is it possible to make a Raycast hit a trigger with a specific tag and spawning a prefab in on the trigger objects exact location? I am trying to make a simple building system using this and i cant seem to get it right, sorry... I am quite new to this :)
It is basically going to be used for a building system, sort of like what you see in rust. I am not sure if this will work or not, but I think it may have a chance :) though as I said I am quite new to all of this. So, is this possible? can you help me with it? (PS here is what I am working with so far).
Thank you.
using UnityEngine;
using System.Collections;
public class BuildSystem : MonoBehaviour {
//
public GameObject BuildMenu;
public GameObject BuildTool;
public GameObject Foundation;
public GameObject Pilar;
public bool buildMenuActive = false;
public bool cbFoundation = false;
public bool cbPilar = false;
public float Range = 2.0f;
RaycastHit hit;
void Start () {
BuildMenu.SetActive(false);
}
void Update () {
buildMenu ();
}
void buildMenu() {
if (Input.GetKey (KeyCode.CapsLock)) {
BuildMenu.SetActive (true);
buildMenuActive = true;
} else {
if (Input.GetKeyUp (KeyCode.CapsLock)) {
BuildMenu.SetActive (false);
}
}
}
public void buildTool_Foundation() {
BuildTool.SetActive (true);
buildPilar ();
}
void buildPilar() {
}
void buildFoundation() {
}
}
Answer by TotalAnnihilation · May 23, 2015 at 10:04 AM
yep, it is. Javascript:
var prefab:GameObject //assign in Obect inspector
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, 10)) {
if(hit.gameObject.tag == "tag") //name the tag as you wish, unsure if it is //hit.collider.gameObject.tag
Instantiate(prefab, hit.gameObject.transform.position, Quaternion.Euler(0,0,0);
}
should be working that way. Read more here http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
It works, but how would i place the object with the same rotation as the trigger?
ins$$anonymous$$d Quaternion.Euler(0,0,0), simply call the hit.gameObject.transform.rotation. Would be like this: Instantiate(prefab, hit.gameObject.transform.position,hit.gameObject.transform.rotation);
It works in one way, but say i want it to have the same rotation as the trigger object? how would i do that?
Sorry, I don't really get what you want. You want it to have the same rotation as the prefab, the thing you are controlling, or the trigger you are looking at? If last, it should work with hit.gameObject.transform.rotation .
You could also do something like
var inst : GameObject = Instantiante(.....)
inst.transform.rotation = desiredrotation;
Your answer
Follow this Question
Related Questions
Spawn a prefab 1 Answer
Give prefab a parent 2 Answers
How can I make an in-game search engine similar to Scribblenauts 1 Answer
Trigger Spawn Something At A Different Location 1 Answer
i need to destroy prefab clones 1 Answer