Hi, im currently following the 3d course, have no previous programming experience.
(as you can probably see below xD)
Now i want to implement a reload system in the ZombieRunner part of the course.
What i have works i think, i have not found any bugs yet at least.
But is this a right way of doing things? tips would be appreciated!
Hi Rusty,
First of all, good job on challenging yourself. Did you test your idea? Does it work? If so, it’s a solution.
Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.
You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.
Hope this helps
See also;
- Forum User Guides : How to apply code formatting within your post
- Forum User Guides : How to mark a topic as solved
Thanks for your reply, yes, so far it seems to work like intended, without errors.
It just felt overly complicated for something as simple as reloading.
Thought id see here if some more experienced people had tips on how to go about this.
Thought id screenshot so it would look like it does in VS for good readability.
Didnt know you can paste a block of code here, so for testing purposes here i go;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Reload : MonoBehaviour
{
//magazineSize needs to be equal to or greater than clip
[SerializeField] int magazineCurrentAmmo = 12;
[SerializeField] int magazineMaxAmmo = 12;
[SerializeField] int totalAmmo = 24;
int reloadAmmoAmount;
void Update()
{
if (Input.GetMouseButtonDown(0) && magazineCurrentAmmo != 0)
{
magazineCurrentAmmo--;
ShootGun();
}
if (Input.GetKeyDown(KeyCode.R) && magazineCurrentAmmo <= magazineMaxAmmo)
{
if (totalAmmo >= magazineMaxAmmo)
{
if (magazineCurrentAmmo == 0)
{
totalAmmo -= magazineMaxAmmo;
magazineCurrentAmmo = magazineMaxAmmo;
}
else
{
reloadAmmoAmount = magazineMaxAmmo - magazineCurrentAmmo;
totalAmmo -= reloadAmmoAmount;
magazineCurrentAmmo = magazineMaxAmmo;
}
}
else
{
if (magazineCurrentAmmo == 0)
{
magazineCurrentAmmo = totalAmmo;
totalAmmo = 0;
}
else
{
reloadAmmoAmount = magazineMaxAmmo - magazineCurrentAmmo;
if (reloadAmmoAmount >= totalAmmo)
{
magazineCurrentAmmo += totalAmmo;
totalAmmo = 0;
}
else
{
totalAmmo -= reloadAmmoAmount;
magazineCurrentAmmo = magazineMaxAmmo;
}
}
}
}
}
void ShootGun()
{
Debug.Log("pew pew");
}
}
After googling a bit, i ran into someone who had a better/shorter solution for a reload system.
In case anyone needs this in the future;
public void Reloadgun()
{
int bulletToReload= bulletsMagazineSize-bulletsMagazine;
if(bulletToReload< bulletsTotal)
{
bulletsTotal = bulletsTotal - bulletToReload;
bulletsMagazine = bulletsMagazine + bulletToReload;
}
else
{
bulletsMagazine = bulletsMagazine + bulletsTotal;
bulletsTotal = 0;
}
}
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.