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);
		}
		
		
	} 
}

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.

@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!