0
votes

I am trying to build a 2D space shooter game where the player will remain constant at the bottom of the screen and will move left and right. When the player fires a bullet(projectile), the bullet is going up just fine, but it is coming back again. Also if two bullets collide, they react to the collision.

I have two questions here:

  1. How can I ensure that the bullet does not come back? (One way is to destroy it after a few seconds, but not sure if this is the right way)

  2. How do I avoid collision between the bullets.

Here is my code snippet:

void Update () {
if (Input.GetButtonDown("Fire1"))
 {

Rigidbody2D clone = (Rigidbody2D)Instantiate(bullet, transform.position + transform.up, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.up * 20);
 }
}

I am very new to Unity and somehow wrote this code by looking into Unity Forums.Any help will be very much appreciated.

Thanks

More details: I have created the bullet and added Rigidbody2D and a BoxCollider2D for this object. Made the bullet object a prefab and I dragged it under the Player object. Now bullet is a child of the Player object. Attached the script above to the Player object.

1

1 Answers

1
votes

To answer your immediate questions:

1) I am not too sure about how you have the scene setup or about using the Unity 2D tools but usually if your bullets are coming back down then I would assume that you still have gravity applied to the rigidbody -- so make sure that you have that unchecked.

2) With Unity3D for objects to interact with each other they need rigidbodies attached. This is vital, say, if you want your lasers to destroy Monster #1. However, you only need one rigidbody attached to an object to have a desired effect. I would suggest removing the rigidbody from your laser and attach rigidbodies to objects ( Monster #1 ) that you want to be affected by the laser fire. ( That should work, if not there are other options -- one using layers and ignoring particular objects on those layers ).

Tips:

Here are some extra things. When you are creating objects and they fly off screen over time they will build up. Imagine you are in a large battle and you instantiated hundreds of lasers -- it will eventually be an issue for performance. So always consider Destroying objects after a certain amount of time.

Also, after looking at your code, I can't tell whether the velocity of the object is based upon its current position and nothing is being added. What I think is happening is when you instantiate an object it may be moving up, but because there is no incrementer, the object ( as it gets higher ) slows until it is at a rate which it is falling. For example this

    clone.velocity = transform.TransformDirection(Vector3.up * 20);

should probably do this:

    clone.velocity += transform.TransformDirection(Vector3.up * 20);

Heres http://docs.unity3d.com/Documentation/ScriptReference/Vector3-up.html, to read up on velocity. The problem is is that you need to continuously apply a constant forward motion so things in motion will tend to stay in motion. If not gravity will pull it down.

Also, heres a bit of code that I've used in the past that's created a pretty cool shooting laser effect thing:

public class Laser : MonoBehaviour {
    public int fireRate = 70;

    void Start(){
        InvokeRepeating("FireLaser", 0.01f, 0.009f);
        Destroy( gameObject, 2.0f );
    }

    void FireLaser(){
        transform.Translate( Vector3.up * fireRate * Time.deltaTime );  
    }
}

edit: it's 3 a.m. and I hate proofreading.