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...
https://www.youtube.com/watch?v=DxROCSN74Uc
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>());
}
}
using UnityEngine; using System.Collections;
public class WeaponInformation : $$anonymous$$onoBehaviour {
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 BulletIn$$anonymous$$agazine ;
public int BulletBackup;
public int $$anonymous$$agazineCanHold;
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 && BulletIn$$anonymous$$agazine > 0) // The Trigger is controlled from Weapon Handling Script which is attached to the player
{
BulletIn$$anonymous$$agazine -=1;
Instantiate (Bullet,BarrelPosition.position,BarrelPosition.rotation);
Debug.Log (BarrelPosition.position);
}
//Reload Script
if (Reload == true && BulletIn$$anonymous$$agazine!=$$anonymous$$agazineCanHold)
{
if(BulletBackup>=($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine))
{
BulletBackup = BulletBackup - ($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine);
BulletIn$$anonymous$$agazine = BulletIn$$anonymous$$agazine + ($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine);
}
if(BulletBackup<($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine))
{
BulletIn$$anonymous$$agazine = BulletIn$$anonymous$$agazine + BulletBackup;
BulletBackup =0;
}
}
Physics.IgnoreCollision(GameObject.FindWithTag ("Player").GetComponent<CapsuleCollider>(), GetComponent<Collider>());
}
}
Hello, i'm having exactly the same problem, did you find a way to fix it ?
Answer by hyperi0n · Jun 28, 2016 at 09:24 AM
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 :)
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.
2) 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 :) Feel free to ask for details, if this interest you :)
using UnityEngine; using System.Collections;
public class WeaponInformation : $$anonymous$$onoBehaviour {
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 BulletIn$$anonymous$$agazine ;
public int BulletBackup;
public int $$anonymous$$agazineCanHold;
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 && BulletIn$$anonymous$$agazine > 0) // The Trigger is controlled from Weapon Handling Script which is attached to the player
{
BulletIn$$anonymous$$agazine -=1;
Instantiate (Bullet,BarrelPosition.position,BarrelPosition.rotation);
Debug.Log (BarrelPosition.position);
}
//Reload Script
if (Reload == true && BulletIn$$anonymous$$agazine!=$$anonymous$$agazineCanHold)
{
if(BulletBackup>=($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine))
{
BulletBackup = BulletBackup - ($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine);
BulletIn$$anonymous$$agazine = BulletIn$$anonymous$$agazine + ($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine);
}
if(BulletBackup<($$anonymous$$agazineCanHold - BulletIn$$anonymous$$agazine))
{
BulletIn$$anonymous$$agazine = BulletIn$$anonymous$$agazine + BulletBackup;
BulletBackup =0;
}
}
Physics.IgnoreCollision(GameObject.FindWithTag ("Player").GetComponent<CapsuleCollider>(), GetComponent<Collider>());
}
}
Hey prosun, can you double check that the transform of the Bullet prefab (sphere) is centered at Position 0/0/0? That could be the Source of the Problem. If that doesnt Work, collision problems are the onlY thing i can think of.
@hyperi0n yes is centered at zero,if transform is the real problem,then the bullets would not instantiate from the right position when the gun is not a chield of my character.... . . actually the bullet is instantiate from the barrel position,but the barrel position is not stable
But in your video it looks like the barrel is correctly positioned at the tip of the gun, no? hmmm, do your bullets have a rigid body component?
Answer by AunShiLord · Jun 25, 2018 at 03:28 PM
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);
Your answer
Follow this Question
Related Questions
Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers
instantiated projectile conflicts 0 Answers
How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers
Making a time bomb 1 Answer
Problems with instantiated objects in a circle formation 0 Answers