Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spaceinvader91/d0170dbc4fbff695f39b210243528852 to your computer and use it in GitHub Desktop.
Save spaceinvader91/d0170dbc4fbff695f39b210243528852 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyWeapon : MonoBehaviour
{
[SerializeField] private GameObject enemyBullet;
public Transform bulletSpawn;
[SerializeField] private GameObject sfxObject;
[SerializeField] private Transform Player;
//audio
private AudioSource[] shootSFXSource;
private AudioSource shoot1_SFX;
private AudioSource shoot2_SFX;
private AudioClip shootClip;
private bool singleShot, muzzleSweep;
private int gunSoundSelection;
private Vector3 bulletDirection;
// private Rigidbody bulletRB;
// Start is called before the first frame update
void Start()
{
ObjectPooling();
shootSFXSource = sfxObject.GetComponents<AudioSource>();
shoot1_SFX = shootSFXSource[0];
// shoot2_SFX = shootSFXSource[1];
gunSoundSelection = Random.Range(0, shootSFXSource.Length);
shootClip = shootSFXSource[gunSoundSelection].clip;
}
[SerializeField]private List<GameObject> bulletObjList = new List<GameObject>();
[SerializeField]private List<EnemyBullet> bulletScriptList = new List<EnemyBullet>();
void ObjectPooling()
{
for (int i = 0; i < 9; i++)
{
var enemyBulletClone = Instantiate(enemyBullet);
bulletObjList.Add(enemyBulletClone);
bulletScriptList.Add(enemyBulletClone.GetComponent<EnemyBullet>());
enemyBulletClone.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
}
public Transform GetPlayerTransform()
{
return Player;
}
public Transform GetBulletSpawnTransform()
{
return bulletSpawn;
}
private int bulletListCounter;
public void ShootAnim() //fire rate controlled by animation speed
{
if (gunSoundSelection == 0)
{ shoot1_SFX.PlayOneShot(shootClip); }
if (gunSoundSelection == 1)
{ shoot2_SFX.PlayOneShot(shootClip); }
if (bulletListCounter >= bulletObjList.Count)
{
bulletListCounter = 0;
}
var currentBullet = bulletObjList[bulletListCounter];
var currentBulletScript = bulletScriptList[bulletListCounter];
currentBullet.SetActive(true);
if (singleShot)
{
currentBulletScript.GetTrajectoryToPlayer();
currentBulletScript.AddForceToBullet();
}
else if (muzzleSweep)
{
// currentBulletScript.GetTrajectoryToTarget();
}
bulletListCounter++;
}
//fire select
public void SingleShot(bool _bool)
{
singleShot = _bool;
}
public void MuzzleSweep(bool _bool)
{
muzzleSweep = _bool;
}
}
using UnityEngine;
public class EnemyBullet : MonoBehaviour
{
[SerializeField] private Rigidbody bulletRB;
[SerializeField] private float bulletSpeed;
[SerializeField] private GameObject barrelObj;
[SerializeField] private GameObject warningParticle;
[SerializeField] private EnemyWeapon parentWeaponScript;
[SerializeField] private Material warningMat;
[SerializeField]private TrailRenderer thisTrail;
private bool playerWarned;
// Start is called before the first frame update
void Start()
{
thisTrail = GetComponentInChildren<TrailRenderer>();
}
private void OnEnable()
{
}
// Update is called once per frame
void Update()
{
RemoveAfterDelay();
// RaycastToTarget();
//if (onTarget && !playerWarned)
//{
// // bulletMat.color = warningBullet;
// var warningRotation = transform.rotation * Quaternion.Euler(warningRotationOffset);
// var warningParticleClone = Instantiate(warningParticle, transform.position, warningRotation);
// warningParticleClone.SetActive(true);
// warningParticleClone.transform.SetParent(null);
// thisTrail.material = warningMat;
// playerWarned = true;
// // play particle and SFX
//}
//else if (!playerWarned)
//{
// // bulletMat.color = defaultBullet;
//}
}
private void OnCollisionEnter(Collision collision)
{
print("enemy bullet hit : " + collision.transform.tag);
gameObject.SetActive(false);
}
private bool onTarget;
[SerializeField] private Color defaultBullet, warningBullet;
[SerializeField] private Vector3 warningRotationOffset;
public void RaycastToTarget() // if raycast will connect with player, create warning
{
int playerMask = 1 << 17;
int obstacleMask = 1 << 0;
int layerMask = playerMask;// + obstacleMask;
RaycastHit hit;
if (Physics.SphereCast(barrelObj.transform.position, 0.05f, bulletDirection, out hit, 50f, layerMask))
{
var tag = hit.transform.parent.tag;
if (tag.Contains("PlayerCapsule"))
{
onTarget = true;
Debug.DrawLine(barrelObj.transform.position, hit.point, Color.red, 0.8f);
}
else
{
onTarget = false;
Debug.DrawLine(barrelObj.transform.position, hit.point, Color.blue, 0.8f);
print(tag);
}
}
else
{
// Debug.DrawLine(spawnPoint.position, (transform.position - bulletDirection) * 20, Color.green, 0.8f);
}
}
[SerializeField] private float accuracyErrorMargin = 1f;
private Vector3 bulletDirection;
private bool fired;
public void GetTrajectoryToPlayer()
{
if (!fired)
{
bulletRB.velocity = Vector3.zero;
transform.position = barrelObj.transform.position.
removeTimer = 0;
warningParticle.SetActive(false);
var playerTrans = parentWeaponScript.GetPlayerTransform();
// var bulletDirVector = (((playerTrans.position - (Random.insideUnitSphere * 0.5f) * accuracyErrorMargin) - transform.position).normalized);
var bulletDirVector = (playerTrans.position - barrelObj.transform.position).normalized;
bulletDirection = bulletDirVector;
}
}
public void GetTrajectoryToTarget(Vector3 bulletTarget)
{
var bulletDirVector = (((bulletTarget - (Random.insideUnitSphere * 0.5f) * accuracyErrorMargin) - transform.position).normalized);
bulletDirection = bulletDirVector;
}
public void AddForceToBullet()
{
bulletRB.AddForce(bulletDirection * bulletSpeed, ForceMode.Impulse);
// RaycastToTarget();
}
private float removeTimer, removeDelay = 10f;
void RemoveAfterDelay()
{
removeTimer += Time.deltaTime;
if (removeTimer >= removeDelay)
{
gameObject.SetActive(false);
fired = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment