Hi! Me and a couple friends are planning to make a FPS game. We are somewhat new to Unity. I know some C#, but I was wondering how I can make he gun shoot at a certain pace (depending on the guns fire rate is how fast it would fire) and how I could just hold down the left mouse button and it will fire? Thanks!
pseudocode:
float fireRate = 0.2;
float fireTime = 0
Update()
{
fireTime -= Time.deltaTime;
if(attacking && fireTime <= 0)
{
fireProjectile();
fireTime = fireRate;
}
}
But don’t you need to instantiate the bullet?
you can use a co routine to delay the execution of firing.
void startFiring()
{
StartCoroutine("fireBullet",0.5f);
}
IEnumerator fireBullet(float delay)
{
//instaniate bullet function
yield return new WaitForSeconds (delay);
StartCoroutine("fireBullet",delay);
}
Since in real life it’s nearly impossible to see bullets, they’re replaced with RayCast trace. For guns like rocket launcher, you’ll need to instantiate projectile.
fireProjectile() is a function which you have to define and put a shooting script into it.
Not advising you to “make a game like pong” first, but you must at least know how the engine works and how to do C# scripting.
Yeah but also in real life your bullet dosent make it to its location immedietly
This will it shoot without end while you use function startFiring
Please don’t respond to six-year-old threads; it’s against forum rules.
Instead, start your own new post… it’s FREE!
How to report your problem productively in the Unity3D forums:
How to understand compiler and other errors and even fix them yourself:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: https://discussions.unity.com/t/481379