- Home /
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
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?
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.
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.
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.
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; }
Your answer
Follow this Question
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