Skip to content

Instantly share code, notes, and snippets.

@Hotrian
Created October 1, 2015 05:14
Show Gist options
  • Save Hotrian/5284977410dbdbec7b09 to your computer and use it in GitHub Desktop.
Save Hotrian/5284977410dbdbec7b09 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class BulletScript : MonoBehaviour {
// Destroy the parent game object when it is no longer visible
void OnBecameInvisible() {
Destroy( gameObject);
}
void OnTriggerEnter2D( Collider2D other) {
// #TODO Explosion effect, sound effects, etc
Destroy( gameObject);
}
}
@Hotrian
Copy link
Author

Hotrian commented Oct 1, 2015

Here is the original Reddit post in case it is removed.

I whipped up a basic AI for you located on pastebin here. You'll need to setup the references in the inspector. It is a basic ship AI that will turn towards it's destination and moves forwards in the direction it is facing. It will target the player if they are within 'Detect Range' and fire if within 'Attack Range' it will approach the player until it is within 'Target Distance' units from the player. If the player moves outside of it's 'Detect Range' it will move to where it last saw the player. If it is left idle (cannot see player, at last known location) it will begin to roam around.

My player ship, enemy ship, and bullet game objects use Sprite Renderers and Box Collider 2Ds, and my bullet also has a Rigidbody 2D with the default settings save for 'Is Kinematic' must be checked. The Collider on your bullet MUST have 'Is Trigger' checked.

Make sure your bullet is a prefab. You can make a game object into a prefab by dragging it from the Scene Hierarchy and dropping it into the Project Hierarchy. If you've done that correctly, you'll get a new asset that looks like a blue cube. Once your game object has been converted into a prefab, you can delete it from the scene and it will still exist as a file. This is handy for objects like bullets that don't normally exist in a scene, but are spawned in, or objects that are uniform.

Here is an example of what your setup should look like. Everything is setup by default except the 3 references on the bottom. Player Ship Reference and Bullet Reference are required, but if you don't supply a Gun Barrel Reference the ship's body will be used instead.

You can select the references by either clicking the little target icon next to the boxes, or by dragging the game objects into their boxes.

Here's the script I used for my bullets which causes them to be destroyed when they collide with something (in my case, the player), or when they leave the visible screen. And my player ship test, which demonstrates how the player ship detects being hit as well.

public class PlayerShipScript : MonoBehaviour {
    void OnTriggerEnter2D( Collider2D other) {
        // #TODO Take damage, hurt effect, etc
    }
}

If you find that your sprites are turning the wrong direction, or facing at the wrong angle, Note that line 52 can be uncommented to draw a line showing the ship's expected course (straight from the ship to the target), and that lines 57, 62, and 80, each have a Vector3 that describes direction, which may not be the same for me as for you. There is also an angle offset on line 57 currently set to -90f for my sprites.

Edit: Also, be sure your player ship and enemy ships are at the same Z. If they are not, it will mess up the distance calculations and the enemy will shoot around the player instead of at them. Sorry if you're not using the default Unity 2D camera.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment