Created
May 11, 2017 19:46
-
-
Save unity3dcollege/fe9d5cd552460213baf091ea7eea816c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class ProjectileLauncher : MonoBehaviour | |
{ | |
[SerializeField] | |
private Projectile prefab; | |
[SerializeField] | |
[Tooltip("Time in seconds between shots")] | |
private float fireDelay = 0.5f; | |
[SerializeField] | |
private AimType aimType = AimType.AtPlayer; | |
private float timer; | |
private Pool pool; | |
private void Start() | |
{ | |
pool = Pool.GetPool(prefab); | |
} | |
private void Update() | |
{ | |
timer += Time.deltaTime; | |
if (timer >= fireDelay) | |
{ | |
timer = 0f; | |
Fire(); | |
} | |
} | |
private void Fire() | |
{ | |
var projectile = pool.Get(transform.position, Quaternion.identity) as Projectile; | |
//var projectile = Instantiate(prefab, transform.position, Quaternion.identity) as Projectile; // without a pool | |
switch (aimType) | |
{ | |
case AimType.AtPlayer: | |
var playerShip = FindObjectOfType<PlayerSideShip>(); | |
if (playerShip != null) | |
{ | |
var target = playerShip.transform.position; | |
var direction = target - transform.position; | |
projectile.SetDirection(direction.normalized); | |
} | |
break; | |
case AimType.Forward: | |
projectile.SetDirection(transform.forward); | |
break; | |
default: | |
break; | |
} | |
} | |
private void OnDrawGizmos() | |
{ | |
Gizmos.DrawRay(transform.position, transform.forward); | |
} | |
} | |
public enum AimType | |
{ | |
AtPlayer, | |
Forward, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment