Skip to content

Instantly share code, notes, and snippets.

@DonHaul
Created October 11, 2016 19:05
Show Gist options
  • Save DonHaul/ed1598563d2f7844000928063798357b to your computer and use it in GitHub Desktop.
Save DonHaul/ed1598563d2f7844000928063798357b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Shoot8Dir : MonoBehaviour {
Vector2 dir;
public float speed=5f;
public GameObject projectile;
public float offsetFromPlayer=1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
dir.x = Input.GetAxisRaw ("Horizontal");
dir.y = Input.GetAxisRaw ("Vertical");
dir.Normalize ();
if (Input.GetKeyDown (KeyCode.E)) {
GameObject GO =Instantiate (projectile, (Vector2)transform.position + dir * offsetFromPlayer, Quaternion.identity) as GameObject;
if (GO.GetComponent<Rigidbody2D> () != null) {
GO.GetComponent<Rigidbody2D> ().velocity = dir * speed;
} else {
Debug.LogWarning ("Bullet must have Rigidbody2D to have velocity");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment