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 :slight_smile:

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 :slight_smile: 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() {



	}
}

Preview of Scene

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 Unity - Scripting API: Physics.Raycast