Witam! Próbuje stworzyć system podnoszenia amunicji. Funkcja wykrywa gracza (* * Debug.Log("1") **) ale nie odpala funkcji dodającej amunicję(** Debug.Log("AddAmmo") ** ) . Poza tym reszta skryptu działa. Z góry dziękuje za pomoc!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AmmoBox : MonoBehaviour
{
public bool AddAmmo = true;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("1");
if (AddAmmo == true)
{
Debug.Log("2");
other.GetComponent<Gun>().AddAmmo();
Debug.Log("2.5");
Destroy(gameObject);
}
if (other.GetComponent<Gun>())
if (AddAmmo == false)
{
Debug.Log("3");
other.GetComponent<Gun>().SubtractAmmo();
Debug.Log("3.5");
Destroy(gameObject);
}
}
}
}
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public Camera FpsCamera;
public float Damage = 10f;
public float Range = 200f;
public float ImpactForce = 60f;
public float FireRate = 10f;
public int MaxAmmo = 210;
public int MagazineMaxAmmo = 30;
public int CurrentAmmo;
public float ReloadTime = 4f;
private bool IsReloading = false;
public int NeededAmmo;
public AudioSource ReloadSound;
public AudioSource ShootSound;
public AudioSource OutOfAmmo;
public AudioSource Audio;
public AudioSource Silence;
public ParticleSystem MuzzyleFlash;
public GameObject ImpactEfect;
private float NextTimeToFire = 0f;
public void AddAmmo()
{
MaxAmmo = MaxAmmo +60;
Debug.Log("AddingAmmo");
}
public void SubtractAmmo()
{
MaxAmmo = MaxAmmo - 31;
Debug.Log("SubtractingAmmo");
}
void Start()
{
Audio = GetComponent<AudioSource>();
CurrentAmmo = MagazineMaxAmmo;
}
void Update()
{
if (IsReloading)
return;
if (MaxAmmo >= 1)
{
if (CurrentAmmo <= 0 || Input.GetKeyDown(KeyCode.R) && CurrentAmmo < MagazineMaxAmmo)
{
StartCoroutine(Reload());
return;
}
}
if (MaxAmmo > 0)
{
if (Input.GetKey(KeyCode.Mouse0) && Time.time >= NextTimeToFire)
{
NextTimeToFire = Time.time + 1f / FireRate;
Audio = ShootSound;
Audio.Play();
Shoot();
}
}
else
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Audio = OutOfAmmo;
Audio.Play();
}
}
IEnumerator Reload()
{
IsReloading = true;
Audio = ReloadSound;
Audio.Play();
Debug.Log("Reload");
yield return new WaitForSeconds(ReloadTime);
if (MaxAmmo >= MagazineMaxAmmo)
{
CurrentAmmo = CurrentAmmo + NeededAmmo;
IsReloading = false;
NeededAmmo = 0;
}
else
{
CurrentAmmo = MaxAmmo;
IsReloading = false;
NeededAmmo = 0;
}
}
void Shoot()
{
MuzzyleFlash.Play();
CurrentAmmo--;
MaxAmmo--;
NeededAmmo++;
RaycastHit Hit;
if (Physics.Raycast(FpsCamera.transform.position, FpsCamera.transform.forward, out Hit, Range))
{
Debug.Log(Hit.transform.name);
Target target = Hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(Damage);
}
if (Hit.rigidbody != null)
{
Hit.rigidbody.AddForce(-Hit.normal * ImpactForce);
}
GameObject ImpactGo = Instantiate(ImpactEfect, Hit.point, Quaternion.LookRotation(Hit.normal));
Destroy(ImpactGo, 2f);
}
}
}
}