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 Clet_ · May 05, 2014 at 11:35 PM

You could put an empty game object child of the rotating part of your turret. Place its local position at the tip of the cannon and add a reference to this gameObject's transform from your turret script.

Make sure the zAxis of the transform is pointing towards the direction you want the bullet to go when fired and then, spawn bullets at this location and add use the empty gameObject's transform.forward to know which way to shoot.

Here's a script example

 using UnityEngine;
 
 public class Bullet : MonoBehaviour {
 
     public float speed;
     public Vector3 direction;
 
     private void Update () {
         transform.position += direction * speed * Time.deltaTime;
     }
 }
 
 public class Turret : MonoBehaviour {
 
     public Transform bulletSpawnT;
     public Bullet bulletPrefab;
     
     private void Update () {
         if(Input.GetKeyDown (KeyCode.Space)) {
             FireBullet();
         }
     }
 
     private void FireBullet () {
         Bullet newBullet = (Bullet)(Instantiate (bulletPrefab,bulletSpawnT, Quaternion.identity) as GameObject);
         newBullet.direction = bulletSpawnT.forward;
     }
 }
Comment
Add comment · Show 3 · 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 Clet_ · May 05, 2014 at 11:39 PM 0
Share

I've just realized you're using RigidBody2D, so my bullet's update isn't appropriate, but I guess you'll be able to figure out by yourself the AddForce part of the problem.

avatar image michellejean · May 05, 2014 at 11:40 PM 0
Share

I do actually have an empty game object attached to the end of the turret. I had to do that so that the bullets would spawn at the end of the gun ins$$anonymous$$d of halfway down the barrel. It is the GetChild of Rigidbody2D newBullet = (Rigidbody2D) Instantiate(Bullet, transform.GetChild(0).position, transform.rotation);

However, its rotation does not change when I move the gun and am watching it in the inspector. The only rotation that changes is that of the pivot.

avatar image Clet_ · May 06, 2014 at 03:53 PM 0
Share

The rotation you see in the inspector is in local space i.e. its rotation according to its parent. If the transform is a child of the turret, it is normal that its rotation stays the same even when the turret's rotation change.

If the transform is setup as I mentioned in my answer, you just have to send a force towards the transform's forward direction by using transform.forward

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

22 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

Related Questions

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

Aiming a turret of arbitrary orientation 1 Answer

Bullet reload problem 3 Answers

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


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