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 SirMcsquizy · Dec 13, 2015 at 07:16 AM · 2d game2d-platformer

Trying to make a 2D firing script!

Hi there!

So I'm trying to get back into Unity after a year break. So me and a friend are making a 2D game. And I am trying to make a gun to fire. However I get the bullet to spawn but for some odd reason its not moving forward!

Someone mind helping me

Gun Code :using UnityEngine; using System.Collections;

public class Randomgun : MonoBehaviour {

 public GameObject Bullet;
 public GameObject TempBullet;
 public Vector3 localPosition;


 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
     if (Input.GetKey(KeyCode.Mouse2))
     {
                     

     TempBullet = Instantiate (Bullet) as GameObject;
     TempBullet.transform.parent = transform;
     //TempArrow.transform.rotation.parent = rotation;
     TempBullet.transform.localPosition = localPosition;
     }

 }

}

And here is the bullet code:

 using UnityEngine;
 using System.Collections;
 
 public class Bullet2 : MonoBehaviour {
     public GameObject Bullet;
     public float maxDist;
     public float speed = 10;
     void Start()
     {
         
         transform.rotation = GameObject.FindGameObjectWithTag ("Gun").transform.rotation;
     }
     
     
     // Update is called once per frame
     void Update () {
         
         transform.position += transform.forward;
         
         if (Vector2.Distance (gameObject.transform.position, GameObject.FindGameObjectWithTag("Gun").transform.position) >= maxDist)
         {
             GameObject.Destroy(Bullet);
         }
         
         
     } 
 }
Comment
Add comment · Show 1
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 LazyElephant · Dec 13, 2015 at 07:34 PM 0
Share

After changing my answer, it occurred to me that maybe the bullet was moving all along, but didn't appear to be because it was moving into the screen. I'd be curious to know if just changing transform.forward to transform.right would solve the problem. I'll try it myself when I get home from work.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyElephant · Dec 13, 2015 at 10:05 AM

Try changing line 18 to transform.Translate( Vector3.forward * Time.deltaTime)

Alternatively, you could use a rigidbody and set it's velocity like they did in this 2d space shooter guide

http://blog.lessmilk.com/unity-spaceshooter-1/

Using this method, your new gun class would look like this:

 public class Randomgun : MonoBehaviour {
 
  public GameObject Bullet;

  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
      if (Input.GetKey(KeyCode.Mouse2))
      {
             Instantiate(Bullet, transform.position, Quaternion.identidy);
      }
  }

and your new bullet class would be

     public class Bullet2 : MonoBehaviour {

          public float speed = 10;

    void Start()
          {         
              GetComponent<Rigidbody>.velocity = Vector3.right*speed;
          }
          
          
          // Update is called once per frame
          public void OnBecameInvisible() {
                  GameObject.Destroy(Bullet);
           
          } 
 
  }

Make sure the bullet prefab has a rigidbody attached to it, the rigidbody has gravity turned off, and that the collider is set to be a trigger. The OnBecameInvisible function will destroy your bullet when it leaves the screen. If you want it to be destroyed before that, you can change it back to an Update function and check the distance again like you were doing before.

Comment
Add comment · Show 6 · 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 SirMcsquizy · Dec 13, 2015 at 03:17 PM 0
Share

@LazyElephant

Yea, it still stays in the same place :/

avatar image LazyElephant SirMcsquizy · Dec 13, 2015 at 06:23 PM 0
Share

Did you check the shooter link I gave you? The method they use is much simpler.

avatar image SirMcsquizy LazyElephant · Dec 13, 2015 at 06:25 PM 0
Share

@LazyElephant

Yea I did...but it looks like they are using Java? Even though it looks like a c# script...

So it's kinda confusing me.

Show more comments
Show more comments
avatar image
0

Answer by Elrien · Dec 13, 2015 at 02:44 PM

@SirMcsquizy hi:) this is what i'm using right now to make the bullet in my game move: movement = transform.up* speed * Time.deltaTime; transform.position += movement;

in the update function. before that you have to create a vector3 movement variable. i don't know what kind of game yours is, but if tranform.up doesn't work, try with different directions. hope this helps!

Comment
Add comment · Show 2 · 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 SirMcsquizy · Dec 13, 2015 at 03:16 PM 0
Share

@Elrien Hi there!!!!!!

The game I am making is a side scrolling, which brings me to my next question.

Where would I put this in my code? Again sorry, It's been a year and i am OUT of practice.

avatar image Elrien · Dec 13, 2015 at 07:07 PM 0
Share

no problem at all:) so what i would do is something like this: using UnityEngine; using System.Collections;

 public class Randomgun : $$anonymous$$onoBehaviour { 
    public float speed; //you make it public so that you can set the value in the inspector
    Vector3 movement;
 
    void Update() {
    //the movement is equal to a vector pointed to the right multiplied by the speed you chose and time.deltaTime
    movement = transform.right * speed * Time.deltaTime; 
 //the movement vector is added to the position (another vector3) of the gameobject this script is attached to (randomGun) 
    transform.position += movement; //you can also write this as transform.position = transform.position + movement;
 }
 }

hope this refreshed your memories. if not, feel free to ask:)

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

33 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

Related Questions

My Player doesn't move anymore. I didn't change a thing in the script and it did work before. What's wrong? 1 Answer

2D Character won't jump diagonally 1 Answer

How to create soil or sand?? 1 Answer

2d platformer script problems 0 Answers

"Create New Palette" doesn't actually create a palette at all 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