Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PixelJam Studios · May 23, 2015 at 07:51 AM · prefabtriggerspawnraycasthit

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() {
 
 
 
     }
 }

Preview of Scene

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Wiki

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PixelJam Studios · May 23, 2015 at 08:34 PM 0
Share

Thanks for the reply, I'll try that asap

avatar image PixelJam Studios · May 23, 2015 at 09:56 PM 0
Share

It works, but how would i place the object with the same rotation as the trigger?

avatar image TotalAnnihilation · May 24, 2015 at 10:23 AM 0
Share

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);

avatar image PixelJam Studios · May 24, 2015 at 02:43 PM 0
Share

It works in one way, but say i want it to have the same rotation as the trigger object? how would i do that?

avatar image TotalAnnihilation · May 24, 2015 at 05:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges