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 Mattprass · Oct 15, 2016 at 06:20 AM · instantiatebulletpatternin front

Instantiating a pattern in front of the player?

I would like to instantiate a pattern of bullets in front of the player, a pattern like so:

 [] []
 [] []
 [] []

My code for the shooting script is this so far:

 public class HoldShoot : MonoBehaviour 
 {
     public HoldBullet hB;
     public Camera cam;
     public Transform player;
     public float spacing = 1f;
 
     private float distance = 3.0f;
 
     void Update () 
     {
         if (Input.GetButtonDown ("Fire1")) 
         {
             for (int y = 0; y < 3f; y++) 
             {
                 for (int x = 0; x < 2f; x++) 
                 {
                     Vector3 pos = new Vector3 (player.position.x+x, player.position.y+y, player.position.z)  *spacing;
                     Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);
                 }
             }
         }
     }
 }

My code for the actual bullet is here:

 public class HoldBullet : MonoBehaviour 
 {
     public float speed = 3.0f;
 
     void Update () 
     {
         StartCoroutine (Example ());
     }
 
     IEnumerator Example()
     {
         yield return new WaitForSeconds (2f);
         transform.position += transform.forward * speed * Time.deltaTime; 
         yield return new WaitForSeconds (5f);
         Destroy (this.gameObject);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag.Equals("Player")) 
         {
             Debug.Log ("player hit");
         }
         else
         {
             Destroy (this.gameObject);
         } 
     }
 }

so right now the Bullet instance spawns but only aligns to one axis, Bullets do not orientate themselves based on player's forward or camera orientation, and my question would be how would one accomplish this?

Comment
Add comment · Show 7
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 Bmarlyman21 · Oct 15, 2016 at 07:54 PM 0
Share

try using quaternion.identity rather than transform.rotation when instantiating. Also make sure your rotations are zeroed out in your bullet prefab.

avatar image Mattprass Bmarlyman21 · Oct 16, 2016 at 05:26 PM 0
Share

sorry about the late reply, and thank you for commenting

unfortunately no dice. the bullet prefab was already zeroed out and changing the rotation has the cubes not turning at all

avatar image Bmarlyman21 Mattprass · Oct 16, 2016 at 05:48 PM 0
Share

Try creating a new empty object and putting it at the end of your gun barrel (making sure it's facing the right direction). Then create a new variable that finds the empty object and use that as your spawn point and rotation when you instantiate. Also try giving your bullet prefab a rigid body and using AddForce rather than transform.position to propel it forward.

Show more comments
avatar image Mattprass · Oct 16, 2016 at 05:33 PM 0
Share

an update has surfaced!

i figured out the reason it was rotating strangely was because when i was facing the x axis, it wasn't correctly identifying that the x axis i was facing now became the z axis, and vice versa.

I added an if statement that sort of makes it work correctly (it can definitely be better)

 if ((cam.transform.forward.x >= 0 && cam.transform.forward.z >= 0) || (cam.transform.forward.x < 0 && cam.transform.forward.z < 0)) {
                     pos = new Vector3 (player.position.x + x, player.position.y + y, player.position.z) * spacing;
                 } else {
                     pos = new Vector3 (player.position.x , player.position.y + y, player.position.z + x) * spacing;
                 }
                 Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);

if anyone has any idea how to make it better, you'd be very greatly appreciated! for now I'm gonna try some stuff on my own and update this question for future references

avatar image Mattprass · Oct 17, 2016 at 01:32 AM 0
Share

bingo! this suited my needs nicely

 if ($$anonymous$$athf.Abs(cam.transform.forward.z) < 0.5f) {
                         pos = new Vector3 (player.position.x , player.position.y + y, player.position.z+x) * spacing;
                     } else {
                         pos = new Vector3 (player.position.x+x, player.position.y + y, player.position.z ) * spacing;
                     }
                         Instantiate (hB, pos +cam.transform.forward *distance, cam.transform.rotation);


Still have no clue as to why it was behaving so strangely, but this put it to a "good enough" status at the very least

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate rotation not working 1 Answer

Instantiated bullets hitting GameObjects? 4 Answers

How to check who shot whom 3 Answers

Enter the Gungeon/Touhou style Bullet Patterns? 0 Answers

Photon Network instantiation 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