Hi, I currently have 2 problems with my shooter game:
- Items fall through the floor
- Items don’t work
This item is referred to as small life energy in my files and it refills your health. I know that for the first problem the reason why it’s doing that is because collision is set to trigger. I would turn it to isTrigger = false but the player needs to be able to walk through it. And yes, gravity is necessary so I added a rigidbody.
My item is a prefab, which means that I can’t attach any script to it(at least I think so). So the item won’t heal the player because it can’t call void Heal from the MMEnergy script.
Shield Attacker
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShieldAttacker : MonoBehaviour
{
public float speed = 4f;
public bool Turning;
public float HP = 4f;
public MegaBuster Charge;
public WVS weapon;
public MMEnergy change;
public float dropChance;
public GameObject smallLE;
public GameObject bigLE;
public GameObject smallWE;
public GameObject bigWE;
public Transform itemSpawn;
// Start is called before the first frame update
void Start()
{
dropChance = Random.Range(1, 128);
Turning = false;
}
void Update()
{
if (Sub_Screen.GameIsPaused) return;
if (Turning == true) return;
transform.Translate(Vector3.forward * Time.deltaTime * speed);
if (HP <= 0)
{
die();
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Ground")
{
Turning = true;
transform.Rotate(180, 0, 0);
StartCoroutine(Turn());
}
if (other.gameObject.CompareTag("Bullet"))
{
if (HP >= 1)
{
if (weapon.weaponSelected == 0)
{
if (Charge.damage == 1)
{
Damage(1);
}
if (Charge.damage == 2)
{
Damage(2);
}
if (Charge.damage == 3)
{
Damage(3);
}
}
if (weapon.weaponSelected == 1)
{
Damage(2);
}
}
else if (HP <= 0)
{
die();
}
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
change.Damage(4);
}
}
IEnumerator Turn()
{
yield return new WaitForSeconds(1);
Turning = false;
}
void Damage(int attack)
{
dropChance = Random.Range(1, 128);
HP -= attack;
}
void die()
{
Destroy(gameObject);
}
void OnDestroy()
{
if (dropChance >= 1 && dropChance <= 13)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance >= 14 && dropChance <= 26)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance >= 27 && dropChance <= 62)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance >= 63 && dropChance <= 72)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance >= 73 && dropChance <= 95)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance >= 100 && dropChance <= 127)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
if (dropChance == 128)
{
Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
}
}
}
Small Life Energy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmallEnergy : MonoBehaviour
{
public MMEnergy refill;
public Rigidbody rb;
void Awake()
{
// Setting up the reference.
GameObject Health = GameObject.Find("Main Camera");
refill = Health.GetComponent<MMEnergy>();
}
void start()
{
this.rb.useGravity = true;
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Destroy(gameObject);
}
if (other.CompareTag("Ground"))
{
this.rb.useGravity = false;
}
}
void OnDestroy()
{
refill.Heal(Random.Range(2, 4));
}
}
Note: I coded a random drop chance for the item, but right now it just drops 1 thing, for debugging reasons.
Thanks in advance!