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 turosteel · Jan 07, 2018 at 02:36 AM · transform.positionchild objectbullet-prefab

Why are my bullets spawning only from the original muzzle transform?

This is for a top-down 2D game:

After successfully spawning bullets from my player instance's transform, now that I disable the bullets as a trigger, there is some collision issues throwing the player around. To solve this, I created a child empty named "Muzzle", on my player object. However, when I fire bullets, they only spawn from the original position of my muzzle object, regardless of where the player moves. I have focused on the muzzle object while testing and it does indeed move with the player, yet the bullets persist on using the first location.

Any help would be very appreciated!

Here's the pertinent code (don't mind the commenting):

 public Player_Controller player_script;
 public Player_Stats stats_script;
 public GameObject bullet;
 public GameObject muzzleObj;

 private Rigidbody2D pRb;
 private float bulletSpeed;
 //private Vector3 offsetPosition;
 private Quaternion offsetRotation;
 private Vector3 muzzle;

 // Use this for initialization
 void Start () {
     //muzzleObj = transform.FindInChildren ("Muzzle").gameObject;
     //muzzleObj = GameObject.Find("Muzzle");
     pRb = player_script.Player1.GetComponent<Rigidbody2D> ();
     bulletSpeed = 500.0f;
 }

 // Update is called once per frame
 void Update () {
     muzzleObj = GameObject.Find("Muzzle");
     //muzzle = transform.Find ("Muzzle").gameObject.transform.position;
     //muzzle = transform.FindInChildren("Muzzle").position;
     muzzle = muzzleObj.transform.position;

     if (muzzle == null) {
         Debug.Log ("null");
     }

     Fire ();
 }

 void FixedUpdate (){
     
 }

 void Fire (){
     if(Input.GetMouseButtonDown(0)){//Left click is 0, right click is 1, middle click is 2
         GameObject bulletClone;
         float maxBulletTime = 500;
         float bulletTime = 0;
         //offsetPosition = new Vector3(pRb.transform.position.x, pRb.transform.position.y, pRb.transform.position.z);
         offsetRotation = Quaternion.Euler (pRb.transform.rotation.x, pRb.transform.rotation.y, pRb.transform.rotation.z);

         //Spawn bullet at Player1's location and with same orientation
         bulletClone = Instantiate(bullet, muzzle, offsetRotation);

         var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         mousePos.z = 0;
         Vector3 bTarget = mousePos - bulletClone.transform.position;
         bTarget = bTarget.normalized;//REMEMBER! Normalizing this prevents the speed to arrive to target point from changing based on how far away target point is from origin point.

         bulletClone.GetComponent<Rigidbody2D>().AddForce(bTarget * (bulletSpeed * Time.deltaTime), ForceMode2D.Impulse);

         /*if(bulletTime == maxBulletTime){
             Destroy(bulletClone);
         }else{*/
             //Add force to the cloned bullet in it's forward direction
         //bulletClone.transform.position = Vector3.MoveTowards(bulletClone.transform.position, bTarget, (bulletSpeed));// * Time.deltaTime));
         //    bulletTime += Time.deltaTime;
         //}
     }
 }

"muzzleObj" is assigned an instance in the editor when I test.

I receive no errors.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by turosteel · Jan 07, 2018 at 06:02 AM

I don't know why....

but it now works....

Strange.

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

122 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

Related Questions

Child object not rotating as parent 0 Answers

Object stops lerping when target is far away 0 Answers

child object transform.position not working properly 0 Answers

Animator Position VS Script Vector 3 0 Answers

Untouchable child objects of first person controller 1 Answer


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