Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by pro-costa · Dec 08, 2019 at 12:08 AM · gameobjectinstantiategunbulletsgun script

Objects instantiate at prefab location instead of playtime location

When I instantiate my bullets from my gun at my gun barrel in the Shoot() function it instantiates at the (0,0,0) location it is on my prefab object. But when I instantiate it in the Update function it instantiates the bullets at the runtime barrel actual location. If I print the barrel position in the Shoot function it says (0,0,0) but in the update function, it does the live position.

using UnityEngine; public class Weapon : MonoBehaviour { public int damage; public int range; public int speed; public Transform barrel; //shotgun public float spread; public int bulletCount;

 public Camera fpsCam;
 public GameObject bullet;
 private void Start()
 {
     barrel = gameObject.GetComponentInChildren<Transform>();
 }
 public void Shoot()
 {
     Transform bt = barrel.transform;
     for (int i = 0; i < bulletCount; i++)
     {
         print("L" + barrel.transform.position);
         var randomNumberX = Random.Range(-spread, spread);
         var randomNumberY = Random.Range(-spread, spread);
         GameObject b = Instantiate(bullet, transform.position, transform.rotation);
         b.transform.Rotate(randomNumberX, randomNumberY, 0);
         Rigidbody rb = b.GetComponent<Rigidbody>();
         rb.AddForce(transform.forward * speed * Time.deltaTime);


     }
 }
 private void Update()
 {
   

 }

}

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 WinterboltGames · Dec 08, 2019 at 07:01 AM 1
Share

This`GameObject b = Instantiate(bullet, transform.position, transform.rotation);` should be this GameObject b = Instantiate(bullet, bt.position, bt.rotation);

avatar image pro-costa WinterboltGames · Dec 08, 2019 at 04:24 PM 0
Share

it is still is instantiating them at (0,0,0) :(

avatar image Larry-Dietz pro-costa · Dec 08, 2019 at 05:00 PM 0
Share

Just a quick guess on my part, but try using localPosition and see if that makes a difference...

 GameObject b = Instantiate(bullet, bt.localPosition, bt.rotation);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lazdude17 · Dec 08, 2019 at 07:00 PM

I would just get rid of the Start function. You name the Transform in Shoot().

 public void Shoot()
  {
      //no need for transform. you only use the position
      Vector 3 bt = barrel.transform.position;

      for (int i = 0; i < bulletCount; i++)
      {
          print("L" + barrel.transform.position);
          var randomNumberX = Random.Range(-spread, spread);
          var randomNumberY = Random.Range(-spread, spread);

          //You made a bt variable for barrel transform and never used it
          GameObject b = Instantiate(bullet, bt.position, Quaternion.Identity);

          b.transform.Rotate(randomNumberX, randomNumberY, 0);
          Rigidbody rb = b.GetComponent<Rigidbody>();
          rb.AddForce(transform.forward * speed * Time.deltaTime);
      }
  }

May be the problem, but Idk how your prefab is set up.

Comment
Add comment · Show 1 · 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 Lazdude17 · Dec 08, 2019 at 07:01 PM 0
Share

And you need to run Shoot() in your Update() so that everytime Shoot() is called it will update bt.

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

200 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 avatar image avatar image avatar image avatar image

Related Questions

How do I make it so that my bullet projectile for my gun does damage? 1 Answer

Bullets going wrong direction 1 Answer

Bullets aren't firing / Bullet holes not instantiating correctly 0 Answers

what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers

Empty gameobject instantiation problem 3 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