Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
0
Question by Philosophbot · Jul 24, 2016 at 08:42 AM · shootingpositioningbulletsplanes

How can I make bullets shoot out of a plane?

I made a plane that can shoot bullets, but they always shoot in one direction. By that I mean when I rotate the plane it keeps shooting the bullets in the same direction. Here is my code for the bullet scripts.

This script spawns the bullet using UnityEngine; using System.Collections;

public class BulletProjectile : MonoBehaviour { public GameObject spawnPosObj; public GameObject Bullet; // Use this for initialization void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         Instantiate(Bullet, spawnPosObj.transform.position, Quaternion.identity);
     }
 }

} This script tells the bullet what to do when it spawns using UnityEngine; using System.Collections;

public class BulletScript : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     transform.Translate(transform.forward * 50 * Time.deltaTime);
   
 }

}

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

Answer by JasonSic · Jul 24, 2016 at 11:53 AM

First off, try formatting your question better so it's easier to understand. With the code snippets out of place it can be confusing.

The fact you use Quaternion.identity when instantiating means each instantiated bullet's rotation is set to (0, 0, 0). Therefore, all bullets you instantiate face the same direction. Since you are having each bullet move by it's transform.forward direction, and each bullet is facing the same direction, each bullet will move in the same direction. Make sense?

One way to make the bullet's travel direction more realistic would be to tell each individual bullet which direction the plane is facing at the moment of instantiation.

Here's how it could be done:

First, the BulletScript:

 public Vector3 travelDirection;
 
 void Update()
 {
     transform.Translate(travelDirection * 50 * Time.deltaTime);
 }

See how the bullet translates by a travel direction instead of it's transform.forward? The travel direction variable is empty but it will be assigned when the bullet is instantiated.

Now, the BulletProjectile:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         GameObject bullet = (GameObject)Instantiate(Bullet, spawnPosObj.transform.position, Quaternion.identity);
         
         bullet.GetComponent<BulletScript>().travelDirection = PlaneObject.transform.forward;
     }
 }

This may not be the best solution in your case, or the most efficient. But hopefully it will give you an understanding of how to accomplish things like this on your own in the future. Bye.

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 Philosophbot · Jul 24, 2016 at 05:33 PM 0
Share

Thank you for your feedback but in this line of code: bullet.GetComponent().travelDirection = PlaneObject.transform.forward;

plane object does not exist. Do you want me to create another gameObject with that name?

avatar image JasonSic Philosophbot · Jul 24, 2016 at 05:45 PM 0
Share

I know PlaneObject doesn't exist for you. I wrote "PlaneObject" as a generic name so you would understand that you need to reference your plane for that line of code.

You just need the plane's forward direction there.

avatar image Philosophbot JasonSic · Jul 24, 2016 at 05:57 PM 1
Share

Thank you for the feedback it really helped.

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

shooting raycast 1 Answer

fps shooting enemy 1 Answer

How to make bullets spread in unity2D? 0 Answers

2D Character Shooting 0 Answers

How to make 2d shooting towards the mouse but have it continue in staright line afterwards? 1 Answer


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