Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Prosun · Jun 28, 2016 at 12:47 PM · instantiate prefab

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>());
 
     }
 
 
 }

Comment
Add comment · Show 6
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 Xaverix · Jun 27, 2016 at 11:08 PM 0
Share

Can you paste your script here?

avatar image Prosun Xaverix · Jul 02, 2016 at 11:54 AM 0
Share

@Xaverix do you have any idea about this????:(

avatar image Prosun · Jun 28, 2016 at 12:25 PM 0
Share

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>());

 }


}

avatar image Prosun · Jul 02, 2016 at 12:04 PM 0
Share

@$$anonymous$$ikeNewall :(.... any Idea???

avatar image Prosun · Jul 02, 2016 at 12:06 PM 0
Share

@crazy$$anonymous$$night any Idea ??? :((

avatar image Alicorne · Jul 28, 2019 at 06:21 AM 0
Share

Hello, i'm having exactly the same problem, did you find a way to fix it ?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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 :)

Comment
Add comment · Show 8 · 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 Prosun · Jun 28, 2016 at 12:25 PM 0
Share

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>());

 }


}

avatar image Prosun · Jun 29, 2016 at 06:04 AM 0
Share

@hyperi0n not woking :(,@hyperi0n not working...:(

avatar image hyperi0n Prosun · Jun 29, 2016 at 07:27 AM 1
Share

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.

avatar image Prosun Prosun · Jun 29, 2016 at 03:10 PM 0
Share

@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

avatar image hyperi0n Prosun · Jun 29, 2016 at 04:55 PM 0
Share

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?

Show more comments
avatar image
0

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);

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

7 People are following this question.

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

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


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