We covered some interesting new approaches in this video. Was there anything that you had questions on?
I have gone for the magical theme, and I have not weapons but magical spells. Every spell has a charge animation, a discharge rendering line and a particle animation on the point of impact. I have had to modify my scripts and the gameObject where they where, and now I donât know how, where or when implement the Public Enum! I have no ammo to do so!
Its all good, if it doesnât fit your game then just keep it as a technique you can use in the future should the need arise.
Thank you. I will watch the videos one more time to really grab the idea and save it for now! Itâs not an easy concept for me to really understand, so much âammoâ words in the video!
I have made my first enum & private class to control potions (my pick-ups) to control the number of potions and their strength; a little like a player inventory. It has been a little hard to understand well enough to use like an inventory, but now works well. Six hours of work, but âIT LIVES!!â
Big fan here Rick (did the 2D course too) and love your style but this felt a little rushed through for me. I feel like this is something that could be a really important concept so do you have any decent resources that we could take a look at? Thereâs usually some links but I couldnât see any. Also, do we cover creating our own classes which we can âextendâ (donât know the right terminology). What I mean by that is I plan to have classes which have similar behaviours and methods but extra functionality within them.
Thanks again for your brilliant lectures mate!
Thanks for the feedback, Iâll look to see how I can improve this.
Hello Rick, please help me resolve the bug which causes scrolling and firing shotgun avoid the delay.
It is bugging me and I am not able to figure it out on my own.
Hello! I made a method which can fix the problem that player can skip cooldown time by switch weapon. I donât know whether it is right, but if I have a shotgun which can shot the second time after 5s, I canât immediately shot it by switch it to another weapon then change it back to shotgun. (English is not my first language, sorry for my grammatical issues)
Here is my code:
private bool allowFire = true;
float timeBetweenShot = 0f;
fireRate: Interval between shots
private void OnEnable()
{
// How long has it been since the last shot
float deltaTime = Time.time - timeBetweenShot;
if (!allowFire)
{
StartCoroutine(CoolDown(deltaTime));
}
}
private IEnumerator CoolDown(float passedTime)
{
// the time you should wait:
// (Fire Rate) - (passedTime)
yield return new WaitForSeconds(fireRate - passedTime);
allowFire = true;
}
In Shoot() method:
allowFire = false;
// the last shot's time
timeBetweenShot = Time.time;
......
yield return new WaitForSeconds(fireRate);
allowFire = true;
timeBetweenShot = 0f;
I changed a fair bit of things to get this to work and Iâm unsure if itâll match up for further parts in the tutorial but, I removed the coroutine for a void, and had the waitforseconds turned into a constantly ticking timer, and having a timer that works independently helps to have the script remember where it left off. Itâs a bit convoluted and harder to follow though so I want a more easy to read script with something like this.
bool canShoot = true;
float currentTime;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
if (ammoSlot.GetCurrentAmmo() > 0 && canShoot == true)
{
Shoot();
}
}
HandleTimer();
}
void HandleTimer()
{
currentTime += Time.deltaTime;
if (currentTime >= timeBetweenShots)
{
canShoot = true;
}
}
void Shoot()
{
canShoot = false;
ammoSlot.DecreaseCurrentAmmo();
PlayMuzzleFlash();
ProcessRaycast();
currentTime = 0;
}
My weapon IEnumerator is public and called from PlayerController script and as I understand thatâs why I donât have problem with canShoot variable. I am using new Input System in this project.
Weapon:
bool canShoot = true;
public bool CanShoot { get { return canShoot; } }
public IEnumerator Shoot()
{
canShoot = false;
if(ammoSlot.AmmoAmount > 0) {
GetComponent<AudioSource>().Play();
PlayMuzzleFlash();
ProcessRaycast();
ammoSlot.ReduceCurrentAmmo();
}
yield return new WaitForSeconds(timeBetweenShots);
canShoot = true;
}
PlayerController:
void OnFire(){
Weapon weapon = GetComponentInChildren<Weapon>();
if(weapon == null) return;
if(weapon.CanShoot == false) return;
StartCoroutine(weapon.Shoot());
}