Instantiate not working correctly (Please help!)
So in this particular game I'm using LateUpdate() to set the position of the players spine, and attached to the players hand is a weapon, under the weapon I have Transforms set up as spawn points that are used for prefab instantiation. Here's the bit of code showing the instantiation.
void SpawnShell () {
if (shellPrefab == null)
return;
if (shellSpawn == null) {
Debug.Log ("Weapon::SpawnWeaponEffects -- For the shell to spawn you need to fill in the 'shellSpawn' slot.");
return;
}
if (shellPrefab.GetComponent<Rigidbody> () == null) {
Debug.LogError ("Weapon::SpawnShell -- For the shell to spawn you need a rigidbody component attached.");
return;
}
GameObject shell = (GameObject)SimplePool.Spawn (shellPrefab, shellSpawn.position, shellSpawn.rotation);
Rigidbody srb = shell.GetComponent<Rigidbody> ();
// FIXME: Hardcode.
srb.AddForce (shellSpawn.forward * Random.Range (1, 3), ForceMode.Impulse);
}
This get's called when the player hit's the Fire1 button. Here's that code...
if (aiming) {
if (Input.GetButtonDown ("Fire1")) {
FireWeapon (new Ray(cameraRig.mainCamera.transform.position, cameraRig.mainCamera.transform.forward));
}
}
The FireWeapon function calls to the weapon and tells it to call the Fire() function, which then spawns the prefabs.
The problem is impossible for me to figure out and here's a picture showing the setup:
And as you can see the transform is a child. And in another script I have the spine rotating in late update:
Ray aimRay = new Ray(cameraRig.mainCamera.transform.position, cameraRig.mainCamera.transform.forward);
Vector3 lookPos = aimRay.GetPoint (50);
spine.LookAt(lookPos);
spine.Rotate (spineOffset, Space.Self);
Debug.Log (currentWeapon.transform.position);
But when the object spawns it spawns at the position that the gun is when im not aiming. even though I look down (it will spawn as if I'm looking straight forward) I really hope you guys can help me.
Answer by sxnorthrop · Mar 29, 2016 at 07:38 PM
And the simple pool is just basically instantiating these objects and putting them into a pool.
Solved this by moving the code: if (Input.GetButtonDown ("Fire1"))
into late update
Your answer
Follow this Question
Related Questions
Problem with instantiated prefabs. Object stays dark 1 Answer
Make a gameobject spawn before enemies spawn? (C#) 1 Answer
How to store a position that is moving to create a prefab to spawn at the exact position 1 Answer
Instantiate prefab if no prefab exists at location 1 Answer
Instantiated prefab trying to keep weird Y position 0 Answers