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 Wuzseen · Jul 18, 2012 at 04:17 PM · prefabsparentinginstantiatingrelative position

Instantiating an Object Relative to its Parent

I'm working on creating code for a gun in my game that fires out a prefab when you right click. I've gone through several ideas and I'm sitting on something that I feel should work, but clearly I'm missing some key intuition.

I am using the first person controller and I've modified it a bit. Its children are still Main Camera and Graphics, however Main Camera is also parent to a Gun Camera (used to stop the gun phasing through objects) and Gun Camera is parent to my gun (vacuumgun henceforth).

What I want to do is instantiate my prefab in front of the gun. Here's my code I'm currently sitting at, some things (the print statement for example) are there simply for debugging purposes.

 else if(Input.GetButton("AltFire") && Time.timeScale == 1 )
 {
  if(!sphereUp)
  {
    var test : Transform = Instantiate(sphereThrowPre, sphereLoc.position + sphereLoc.forward*50 , Quaternion.identity );
    test.gameObject.layer = 8;
    test.parent = GameObject.Find("Main Camera").transform;
    print("Instantiated at " + (sphereLoc.position + sphereLoc.forward*2));
         sphereUp = !sphereUp;
   }
 }
 // Not Implemented yet
 else if(sphereUp)
 {
      //apply force to sphere in forward direction "shooting" the ball
 }
 

sphereUp is a boolean that checks if there is already a ball instantiated. sphereThrowPre is the prefab I want to instantiate. sphereLoc is the gun's position.

My logic so far is that I find the location of the gun and add a vector in the forward direction of the gun. It's at a power of 50 right now just to exaggerate the effect in my game for debugging purposes. Then I set the layer of the ball to what Gun Cam sees (so it doesn't phase through walls) and I make the parent the main camera so it moves with the main camera.

According to my print statments I should be seeing my instantiated prefab right in front of me (it's just a sphere primitive with a material and rigidbody on it at this point in time). I'm linking the prefab and gun as transforms.

Yet I don't see the object in front of me, or anywhere at all for that matter.

I'm not sure where exactly I'm going wrong. The not implemented code I posted is where I'm going next with it, but I'm working on instantiating the sphere in the right spot first. That code would be destroying the sphere or deparenting it from the camera and applying a force in the forward direction of the gun to the sphere.

EDIT: Formatting got wayyy messed up

Comment
Add comment · Show 2
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 whydoidoit · Jul 18, 2012 at 04:56 PM 0
Share

Have you tried looking at the scene view when the game runs - finding the instantiated prefab in the hierarchy and seeing where it is in your scene?

avatar image Wuzseen · Jul 18, 2012 at 05:23 PM 0
Share

That's a really good idea, that is a feature I haven't been taking advantage of while learning unity. See the answer I'm about to post though, I solved the issue by kind of taking it "back to formula" so to speak.

2 Replies

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

Answer by jdwieber · Jul 18, 2012 at 05:39 PM

Try attaching an empty game object to your vacuumgun with the offset translation that you want the projectile to have when you instantiate it and use that for the instantiate position. It will move with your gun and save you from doing the math at runtime. It will also give you something to look at when designing the game in the editor.

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 Wuzseen · Jul 18, 2012 at 07:06 PM 0
Share

I ended up doing this and typing up the same thing apparently right around the time you did! Thanks for pointing it out more succinctly. I spent so long trying to code it as I was before which was a huge over complication.

avatar image
0

Answer by Wuzseen · Jul 18, 2012 at 05:41 PM

So, I managed to fix it by reconsidering my options. I placed an empty game object in the spot I want to spawn the sphere and parented it to the main camera and kablam! Here's the code.

 else if(Input.GetButton("AltFire") && Time.timeScale == 1 )

{ if(!sphereUp) { throwSphere = Instantiate(sphereThrowPre, spawnLoc.position , Quaternion.identity ); throwSphere.gameObject.layer = 8; throwSphere.parent = GameObject.Find("Main Camera").transform; sphereUp = !sphereUp; } } else if(sphereUp) { throwSphere.gameObject.layer = 0; if(throwSphere.position.y <= .75) throwSphere.position.y = .75; throwSphere.parent = null; throwSphere.rigidbody.useGravity = true; throwSphere.rigidbody.isKinematic = false; throwSphere.rigidbody.AddForce(GameObject.Find("Main Camera").transform.forward * 2000); sphereUp = !sphereUp; }

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parenting on a network. 0 Answers

Where is Actually The Prefabs Stored Before Instantiation? RAM or Storage(Internal Memory) 2 Answers

Points attached to a prefab 0 Answers

Editor randomly unparents game object after InstantiatePrefab() & SetParent() in OnInspectorGUI() 0 Answers

Problems with nested prefabs 4 Answers


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