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 Damonic217 · Jan 16, 2018 at 11:33 AM · 2d-platformershooting

Shooting a bullet with the same rotation as the gameobject that spawns it. 2D

Greetings,

I've created a little 2D tank and the barrel of the tank follows the player always pointing at a 45 degree angle. At the end of that barrel I have put an empty game object which spawns the bullets, however the bullet doesn't seems to get the rotation of the gameobject, it simply fires up.

This is my barrel game object script.

   public class MultiShoot : MonoBehaviour {
     
         public GameObject bullet;
     
         public float startShotTime = 0.5f;
         public float delayShotTime = 0.5f;
         // Use this for initialization
         void Start()
         {
             InvokeRepeating("ShootMissle", startShotTime, delayShotTime);
         }
     
         // Update is called once per frame
         void Update()
         {
     
         }
     
         void ShootMissle()
         {
             Instantiate(bullet, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.80f), Quaternion.identity);
         }
     }



This is the bullet script

 public class MultiBullet : MonoBehaviour {
 
     float speed = 5f;
     private Rigidbody2D rb;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody2D>();
         
     }
     
     // Update is called once per frame
     void Update () {
         rb.velocity = transform.up * speed;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         //Add explosion Effect
         if (collision.gameObject.tag == "Player")
         {
             // Add game over screen here
             Debug.Log("Player Killed");
             Destroy(gameObject);
         }
         Debug.Log("BOOM");
         Destroy(gameObject);
     }
 }


I do want the bullet to go in a straight line but in proportion to the angle of the game object.

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
4
Best Answer

Answer by Dray · Jan 16, 2018 at 04:02 PM

Look at this line:

 Instantiate(bullet, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.80f), Quaternion.identity);

Especially "Quaternion.identity"; Quaternions are the way to define rotations in Unity. The constant "Quaternion.identity" describes a rotation of zero into all directions, the initial value of every Quaternion so to speak. The reason why your bullets are all flying into the same direction is that they are all spawned with the same (zero) rotation.


To solve this you could simply copy your gameObjects rotation (~quaternion) into the freshly instantiated bullet:

 Instantiate(bullet, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.80f), gameObject.transform.rotation);

Btw I think the bullet should rather move forward than up but that depends

Comment
Add comment · Show 5 · 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 Damonic217 · Jan 17, 2018 at 07:10 AM 0
Share

Thanks for your reply.

However this still doesn't work, I've changed the "transform.up" to "transform.forward".

I was wondering if i had this set up wrong. Currently I just have an empty gameobject with the barrel script attached which fires the bullets, So to test i'm rotating the z-axis of the empty object, however It's still firing straight up ins$$anonymous$$d of the desired rotation.

avatar image Dray Damonic217 · Jan 17, 2018 at 10:26 AM 0
Share

That's quite weird.. Can you add this to your ShootBullet function:

 var bullet = Instantiate(bullet, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.80f), gameObject.transform.rotation);

 Debug.DrawRay(bullet.transform.position, bullet.transform.forward, Color.red, 10f);

and see if the red line points into any other direction than up?

avatar image Damonic217 · Jan 17, 2018 at 11:48 AM 0
Share

Your original answer seemed to work I think it never updated on unitys side when I saved it. Thank you so much !

avatar image Dray Damonic217 · Jan 17, 2018 at 03:11 PM 0
Share

With pleasure :) $$anonymous$$eep it up!

avatar image rodiri · Apr 04, 2021 at 04:47 PM 0
Share

I think the bullet should rather move forward than up

With that comment, you saved me from getting crazy. My bullets were going out at 90 degrees. Your comment made me realize, when creating the "cannon", I created it ai$$anonymous$$g up and then rotated downwards.

Thanks a lot

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

79 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

Related Questions

I am trying to change the bulletPrefab OnCollisionEnter2D but its not working now that I changed to fire at mouse position 0 Answers

Help rotating gun towards mouse position (with flipping) 1 Answer

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

Player Flipping and Shot/Tilemap bugs 0 Answers

How can i do? (enemy don't refind player game object) 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