Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 michellejean · May 05, 2014 at 11:11 PM · rotationbulletsturrets

Getting Bullets to fire in Direction of Gun Barrel

I am making a game which involves two turrets which rotate 360 degrees and fire when I press the space bar. I am stuck with making the bullets actually fire in the direction the turret is facing. I am trying to get the rotation from the parent object of the turret because that part that actually swivels. When I am running the game and I rotate the turrets using the keyboard it is the pivot (the parent object of the turret) y-axis which changes. I need to find a way to get the rotation of the y-axis and have the bullet fire in that direction. The way I am currently trying to get the rotation is leaving me with numbers which are barely above zero (might as well be zero) even though I can see in the inspector that the y value is changing so obviously I am grabbing the wrong value.

     using UnityEngine;
     using System.Collections;
      
     public class TurretScript : MonoBehaviour {
      
         public Rigidbody2D Bullet;
         public float bulletSpeed = .01f;
         private float turretRotationSpeed = 5f;
         void Update () {
             if(Input.GetKey(KeyCode.S) && gameObject.tag == "T1")
                 counterClockwise();
             if(Input.GetKey(KeyCode.J) && gameObject.tag == "T2")
                 counterClockwise();
             if (Input.GetKey (KeyCode.F) && gameObject.tag == "T1")
                 clockwise ();
             if (Input.GetKey (KeyCode.L) && gameObject.tag == "T2")
                 clockwise ();
             if (Input.GetKey (KeyCode.Space)) {
                 fire ();
             }
         }
     fire(){
         Rigidbody2D newBullet = (Rigidbody2D) Instantiate(Bullet, transform.GetChild(0).position, transform.rotation);
         float xDir = transform.parent.rotation.x;
         float yDir = transform.parent.rotation.y;
         Debug.Log (xDir + "    " + yDir);
         Vector2 force = new Vector2 (xDir * bulletSpeed, yDir * bulletSpeed);
         newBullet.AddForce(force);}
     }
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

Answer by robertbu · May 06, 2014 at 07:59 AM

Yep, the way you are doing it will give you values in the range of -1 to 1. Transform.rotation is a Quaternion...a 4D, non-intuitive class. It's x,y,z,w components are not angles. You can use Transform.eulerAngles to get angles, but be warned that their representation is not always what you expect.

Unless your bullet is a sphere, there are two kinds of issues here: the rotation of the bullet, and the direction to fire the bullet. If you've constructed your bullet so that the 'forward' of the bullet is facing positive 'z' when the rotation is (0,0,0), then you can change your Instantiate on line 23 to:

     Rigidbody2D newBullet = Instantiate(Bullet, transform.GetChild(0).position, transform.parent.rotation) as Rigidbody;

Then get rid of lines 23 - 27, and do your AddForce() like:

 newBullet.AddForce(transform.parent.forward * bulletSpeed);

or if you've rotated the bullet to face the correct direction, a more typical solution would be:

 newBullet.AddForce(newBullet.transform.forward * bulletSpeed);

Note that AddForce() does set any speed, and if you left the mass at the default 1.0, it takes a pretty good jolt of force to get something moving. You want to set bulletSpeed to 500 in the inspector to start.

Final note, your code hints at a problem we see on this list: the projectile hitting part of the tank when it is fired. If you have that problem, see Physics.IgnoreCollision().

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

20 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

Related Questions

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

Trying to make a bullet fire at the mouse using transform.up 0 Answers

Clamped Turret Doesn't Want to Lerp the Other Way 2 Answers

How to make a turret follow the center of the screen when it's rotate ? 2 Answers

Flip over an object (smooth transition) 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