Hello there everyone. Today I am working on my ammo pick ups for my game! I have the object being picked up and everything, now I just need it to modigy/add ammo to my guns. They all use the same ammo etc right now so don’t need that. Just need a way when I enter the collider, it not only destorys the object but adds the Ammo. Going to put my Gun script which holds my ammo and then my poweruptrigger script.
GUN SCRIPT:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public LayerMask collisionMask;
public float gunID;
public GunType gunType;
public float rpm;
public float damage = 1;
public int totalAmmo = 40;
public int ammoPerMag = 10;
// Components
public Transform spawn;
public Transform shellEjectionPoint;
public Rigidbody shell;
private LineRenderer tracer;
[HideInInspector]
public GameGUI gui;
// System:
private float secondsBetweenShots;
private float nextPossibleShootTime;
public int currentAmmoInMag;
private bool reloading;
void Start() {
secondsBetweenShots = 60/rpm;
if (GetComponent<LineRenderer>()) {
tracer = GetComponent<LineRenderer>();
}
currentAmmoInMag = ammoPerMag;
if (gui) {
gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
}
}
public void Shoot() {
if (CanShoot()) {
Ray ray = new Ray(spawn.position,spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast(ray,out hit, shotDistance, collisionMask)) {
shotDistance = hit.distance;
if (hit.collider.GetComponent<Entity>()) {
hit.collider.GetComponent<Entity>().TakeDamage(damage);
}
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
currentAmmoInMag --;
if (gui) {
gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
}
audio.Play();
if (tracer) {
StartCoroutine("RenderTracer", ray.direction * shotDistance);
}
Rigidbody newShell = Instantiate(shell,shellEjectionPoint.position,Quaternion.identity) as Rigidbody;
newShell.AddForce(shellEjectionPoint.forward * Random.Range(150f,200f) + spawn.forward * Random.Range(-10f,10f));
}
}
public void ShootContinuous() {
if (gunType == GunType.Auto) {
Shoot ();
}
}
private bool CanShoot() {
bool canShoot = true;
if (Time.time < nextPossibleShootTime) {
canShoot = false;
}
if (currentAmmoInMag == 0) {
canShoot = false;
}
if (reloading) {
canShoot = false;
}
return canShoot;
}
public bool Reload() {
if (totalAmmo != 0 && currentAmmoInMag != ammoPerMag) {
reloading = true;
return true;
}
return false;
}
public void FinishReload() {
reloading = false;
currentAmmoInMag = ammoPerMag;
totalAmmo -= ammoPerMag;
if (totalAmmo < 0) {
currentAmmoInMag += totalAmmo;
totalAmmo = 0;
}
if (gui) {
gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
}
}
IEnumerator RenderTracer(Vector3 hitPoint) {
tracer.enabled = true;
tracer.SetPosition(0,spawn.position);
tracer.SetPosition(1,spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}
PowerUpTrigger Script:
using UnityEngine;
using System.Collections;
public class PowerUpTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter(Collider other)
{
Debug.Log ("Entered Collider!~" + gameObject.name);
if(gameObject.name.StartsWith("AmmoPowerUp"))
{
Debug.Log ("Entered");
GameObject.Destroy(gameObject);
//add a bonus here
}
//Do for each power up
}
}
I can’t figure out how to do this