GeorgeGularte
[email protected]
  • Home
  • Blog
  • Projects
  • Resume

Sample Code Basswings

The Bullet Manager and Bullet
bulletmanager.cs
File Size: 1 kb
File Type: cs
Download File

bullet.cs
File Size: 1 kb
File Type: cs
Download File

Bullet Manager

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public  class BulletManager : MonoBehaviour 
{
  	public  Bullet[]     	m_bullets;
	
	static BulletManager 	s_BULLETMANAGER;
	int						m_bulletIdx;
	
	public GameObject m_PlayerOneBullet;
	
	
	// Use this for initialization
	void Start () 
	{
		s_BULLETMANAGER = this;
		InitializeBulletManager();
		m_bulletIdx = 0;
	}
	
	public static BulletManager Get()
	{
		return s_BULLETMANAGER;
	}
	
	void Update()
	{
		
	}
	
	//Private
	void InitializeBulletManager()
	{
		const int iMaxRegBullets = 30;
		m_bullets = new Bullet[iMaxRegBullets];
		
		GameObject go = new GameObject();
		go.transform.position = Vector3.zero;
		go.name = "BulletPool"; 
		
		for(int i = 0; i < iMaxRegBullets; ++i)
		{
			//GameObject tempBullet =  GameObject.Instantiate( Resources.Load("RegBullet"))as GameObject;
			GameObject tempBullet =  GameObject.Instantiate( m_PlayerOneBullet)as GameObject;
			tempBullet.name += i;
			m_bullets[i] = tempBullet.GetComponent();
			
			tempBullet.transform.parent = go.transform;
		}
	}
	
	public  void FireRegBullets(Vector3 startPos, Vector3 Direction, float Accel)
	{
		int start = m_bulletIdx++;
		m_bulletIdx %= m_bullets.Length;

		do
		{
			if(!m_bullets[m_bulletIdx].gameObject.activeSelf)
			{
				m_bullets[m_bulletIdx].Activate(startPos,Direction, Accel);
				return;
			}
			m_bulletIdx++;
			m_bulletIdx %= m_bullets.Length;
		} while (m_bulletIdx != start);
		
		print("No bullets available");
	}
}

TOP

Bullet Class

NOTE: Delete or Comment out any audio or trail references, if you are going to use this script
using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour 
{
	public const  float c_fAccel = 2f;
	
	public const  float c_fTimeOutTimer = 2f;
	
	public float m_fCountDown;
	
	public Vector3 m_vPos;
	
	public Vector3 m_vVel;
	
	TrailRenderer m_trail;
	
	public string m_sTag;
	
	int m_iDmg;
	
	public AudioClip m_soundEffect;
	AudioSource m_audioSource;
	void Awake()
	{
		m_iDmg = 1;
//		if (!m_trail)
//		{
//			m_trail = GetComponent();
//			m_trail.enabled = false;
//		}
		m_audioSource = GetComponent();
		m_audioSource.playOnAwake = false;
		if(m_soundEffect != null) 
			m_audioSource.clip = m_soundEffect;
		Reset();
	}
	
	void Update () 
	{
		m_fCountDown -= Time.deltaTime;
		if (m_fCountDown < 0)
			Reset();
			
	}
	
	void FixedUpdate()
	{
		
		m_vPos += m_vVel * Time.fixedDeltaTime;
		transform.position = m_vPos;
	}
	
	public void Activate(Vector3 pos, Vector3 dir, float Accel)
	{
		m_fCountDown = c_fTimeOutTimer;
		m_vPos = pos;
		transform.position = m_vPos;
		
		transform.forward = dir.normalized;
		m_vVel = (dir.normalized * Accel) * c_fAccel;
		
		gameObject.SetActive(true);
		
		m_audioSource.Play();
		if(m_trail)
		{
			//m_trail.enabled = true;
			//m_trail.time = 0.1f;
		}
	}
	
	public void OnTriggerEnter(Collider collision) 
	{
		if(collision.gameObject.tag == "Enemy")
		{
			collision.gameObject.GetComponent().ApplyDamage(m_iDmg);
			Reset();
		}
	}
	
	 public void Reset()
	{
		gameObject.SetActive(false);
		
//		if(m_trail)
//		{
//			m_trail.time = 0.0f;
//			m_trail.enabled = false;
//		}
	}
}

TOP
Powered by Create your own unique website with customizable templates.