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
4
Question by Mattivc · May 20, 2010 at 09:35 AM · instantiateraycastprefabchildjavascript-specific

Instantiate as children of GameObject hit by raycast.

I have made a script that instantiates a prefab at the location of a raycasthit. But how can i make it so this prefab also becomes a children of the gameobject it hits. So it will move with the gameobject.

My code looks like this do far:

var linePrefab : GameObject; var decalPrefab : GameObject;

function Shoot () { var rayDirection = SprayDirection(shotSpread);

     if(Physics.Raycast(transform.position, rayDirection, hit, range)) {

         if(hit.distance > minTrailDistance){
             newLineObject = Instantiate(linePrefab);
             newLine = newLineObject.GetComponent(LineRenderer);

             newLine.SetPosition(0, transform.position);
             newLine.SetPosition(1, hit.point);
         }

         Instantiate(decalPrefab, hit.point, Quaternion.LookRotation(hit.normal));//This instantiates the prefab i want to make a child of the raycasthit object.

         hit.collider.gameObject.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);

         if (hit.rigidbody){
             hit.rigidbody.AddForceAtPosition(rayDirection * force,hit.point);
         }
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by duck · May 20, 2010 at 09:48 AM

You need to capture the result returned by your Instantiate function in a variable. The result returned by a call to Instantiate is a reference to the newly instantiated item.

Once you've got this reference in a variable, you can then manipulate it using that variable as shown below:

//This instantiates the prefab i want to make a child of the raycasthit object: var decalInstance : GameObject = Instantiate(decalPrefab, hit.point, Quaternion.LookRotation(hit.normal));

//This parents the object to the object which was hit: decalInstance.transform.parent = hit.transform;

Comment
Add comment · Show 2 · 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 uni · Aug 20, 2010 at 11:53 AM 2
Share

This is something that needs to be implemented in Version 3 of Unity. It should be possible to instantly "Instantiate as child" of the current transform, i.e. transform.Instantiate() should put the clone in local space and Instantiate() should be in world transform space. I think this functionality is at the core of what prefabs are about: i.e., not having to write a bunch of code for something that is intuitive.

avatar image Mattivc · Apr 12, 2011 at 01:41 PM 0
Share

I have added this to the Unity feedback page: http://feedback.unity3d.com/forums/15792-unity/suggestions/1418475-instantiate-as-child-function

avatar image
0

Answer by JMS · Dec 07, 2010 at 02:18 PM

I have been searching all over for this solution. thx!! :D

Comment
Add comment · Show 1 · 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 Mattivc · Apr 12, 2011 at 01:41 PM 1
Share

This belongs as a comment, not its own answer.

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

No one has followed this question yet.

Related Questions

Instantiate prefab from original position to click mouse point position 0 Answers

Help - Instantiate A Prefab On Mouse Position 1 Answer

Spawn Shield to all objects 2 Answers

Give prefab a parent 2 Answers

Instantiating prefabs as child of a gameobject 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