Instantiated at wrong position

bullet from the gun instantiate at right position when the gun is not a child of my player,but when i picked up the gun the bullet instantiate from different position…

this is the link of the video which i’m facing right now…

I'm not able to reply on you comment....!!!!

here is my script

using UnityEngine;
using System.Collections;

public class WeaponInformation : MonoBehaviour {

	public Vector3 LocalPositionInHand =  new Vector3(-0.027615f, -0.10848f, -0.08597f);
	public Quaternion LocalrotationInHand = Quaternion.Euler(-13.8380f, 32.90746f, 172.209f);
	public int GunType;//	5=throwable  ,  1 = handgun , 2 = assultgun

	public int BulletInMagazine ;
	public int BulletBackup;
	public int MagazineCanHold;
	public GameObject Bullet;        // Drag the sphere to the Bullet
	public Transform BarrelPosition; // Drag the barrel to the BarrelPosition

	public bool Trigger = false;
	public bool Reload = false;
	public bool AutoFire = true;



	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		//Fire Script
		if (Trigger == true && BulletInMagazine > 0)  // The Trigger is controlled from  Weapon Handling Script which is attached to the player
		{

			BulletInMagazine -=1;
			Instantiate (Bullet,BarrelPosition.position,BarrelPosition.rotation);
			Debug.Log (BarrelPosition.position);

		
		}

		//Reload Script
		if (Reload == true && BulletInMagazine!=MagazineCanHold) 
		{
			if(BulletBackup>=(MagazineCanHold - BulletInMagazine))
			{
			BulletBackup = BulletBackup - (MagazineCanHold - BulletInMagazine);

			BulletInMagazine = BulletInMagazine + (MagazineCanHold - BulletInMagazine);

			}
			if(BulletBackup<(MagazineCanHold - BulletInMagazine))
			{


				BulletInMagazine = BulletInMagazine + BulletBackup;
				BulletBackup =0;
				
			}
		}



















	
		Physics.IgnoreCollision(GameObject.FindWithTag ("Player").GetComponent<CapsuleCollider>(), GetComponent<Collider>());

	}


}

It’S difficult to diagnose the problem without seeing your script. But I once had a very similar problem so I will try to explain how I solved it:
Instead of using “Instantiate(Object original, Vector3 position, Quaternion rotation);” in the follwing way:

Instantiate(bulletPrefab, barrel.transform.position, barrel.transform.rotation);

I did the following:

var bullet = Instantiate(bulletPrefab);
bullet.transform.position =  barrel.transform.position;
bullet.transform.rotation =  barrel.transform.rotation;

You would obviously have to adapt this to your script. Hope this helps, otherwise you will have to post your script :slight_smile:

Two more thoughts:

  1. Depending on how your bullets are set up, that might have something to do with the problem. Are they rigid bodies? Since the bullets overlap with each other, collisions between bullets might be creating issues. To solve this, implement a simple delay after each shot in the sense of:

    if (Time.time > nextShot){
    Instantiate()… //create the bullet
    nextShot = Time.time + shotDelay;
    }

where nextShot and shotDelay would be defined as floats at the beginning of the script and you would give shotDelay a value of e.g. 0.2 so that a 5 shots would be fired per second. This would be sensible to do even if rigidbodies/collisions is not your current problem.

  1. Instantiating bullets is a great way for quick prototyping, but in the long run it will be a good idea to have a “pool” of bullets available for reuse. instead of creating a bullet when it’s fired and deleting it when it hits something, you would pick a bullet from the list, activate it and again deactivate it when it hits something. This is much more performance-friendly :slight_smile: Feel free to ask for details, if this interest you :slight_smile:

@hyperi0n not woking :(,@hyperi0n not working…:frowning:

It is important to remember, when setting local position to child to use .transform.localPosition and not just transform.position.
Or use public void Translate(Vector3 translation, Space relativeTo = Space.Self);