- Home /
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);
}
}
}
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.
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.
Yea, it still stays in the same place :/
Did you check the shooter link I gave you? The method they use is much simpler.
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.
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!
@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.
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
![](https://koobas.hobune.stream/wayback/20220612052428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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