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 /
avatar image
0
Question by joelluckett83 · Jan 04, 2020 at 03:32 PM · transformprefabscript.

Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D)

If I don't make the Bullet Spawn position a child of my gun it spawns bullets in the correct position but when i make it a child of my gun it spawns in random positions.

Here's the bullet script:

 public class GunScript : MonoBehaviour
 {
     public GameObject BulletPrefab;
     public Transform BulletSpawn;
 
     int Firing;
 
     // Start is called before the first frame update
     void Start()
     {
         //BulletSpawn = GameObject.FindWithTag("BulletSpawn").GetComponent<Transform>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButton("Fire1") && Firing == 0)
         {
             //GameObject bullet = Instantiate(BulletPrefab, BulletSpawn.position, Quaternion.identity) as GameObject;
             //bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 100);
             StartCoroutine(Fire());
         }
     }
 
     IEnumerator Fire()
     {
         Firing = 1;
         GameObject bullet = Instantiate(BulletPrefab, BulletSpawn.position, BulletSpawn.rotation);
         //bullet.transform.position = BulletSpawn.position;
         yield return new WaitForSeconds(0.15f);
         Firing = 0;
     }
Comment
Add comment · Show 3
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 SirCrazyNugget · Jan 05, 2020 at 04:24 AM 1
Share

If you've got the bullet spawn point as the child then try using BulletSpawn.localPosition ins$$anonymous$$d of BulletSpawn.position, similarly with rotation.

avatar image joelluckett83 SirCrazyNugget · Jan 05, 2020 at 10:14 AM 0
Share

I've tried this, it spawns in a random position that is not where it is set

avatar image Ady_M · Jan 05, 2020 at 04:47 PM 1
Share

As a test, track the position of BulletSpawn.
In update, when a specific key (a dev key of your choice) is pressed output BulletSpawn.position to the console.

If the player character moves constantly in your game, disable that feature temporarily.

Stand still and press the dev key a couple of times (check that the position stays the same because it should).

$$anonymous$$ove and rotate a bit then stand completely still and press the dev key a couple of times again.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LOSTSOUL86 · Jan 06, 2020 at 04:49 AM

Dear, I dont see where you assign bullet as a child. Maybe you should send 2 versions of code. One that is working and one that is not working.

I think the problem is how you assign the parent to the child. Do you use method parent or set parent? and when you change child coordinates, after assigning parent or before.

What I would suggest is to change your bullet coordinates first to your gun coordinates. And then just change parent with parent method.

But more I would be able to say after seeing the code.

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

196 People are following this question.

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

Related Questions

transforming a prefab randomly at runtime 3 Answers

How to reference a prefab to the script that doesn't exist in Hierarchy 0 Answers

Prefab Puzzle 1 Answer

Saving customized transform in game 1 Answer

Why Transform and not Prefab? 2 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