- Home /
How to avoid speed change in bullets while moving?
I've seen many examples about shooting in unity using the physics approach and the transform too but something I was wondering is how to prevent bullets from changing speed while moving.
The Space Shooter Project by Unity guys is an example and you can download for free from the asset store. Here is the link in someone is interested:
As you can see in the example or in general, if you shoot without pressing any up/down key the bullets look with one speed but as soon as you press upwards while shooting the projectiles increse speed and vice versa while pressing backwars, I mean the bullets slow down until you get stuck at the bottom of the screen when the speed of the projectiles looks the same as you where not pressing any key while shooting.
A diagram would be:
Only Shooting : BULLET space space BULLET space space BULLET (Normal Speed)
Shooting + UP : BULLET space BULLET space BULLET (Fastest Speed)
Shooting + DOWN: BULLET space space space space BULLET space space space space BULLET (Slowest Speed)
Thanks in advance and sorry for my english I'm still learning ;-)
Answer by AnXgotta · Feb 18, 2014 at 02:21 PM
What I think you are talking about is fire rate. If we look at the code that you linked, in the file called "Shooting Shots" there is this block that controls the shooting:
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
audio.Play ();
}
}
This is the important line:
if (Input.GetButton("Fire1") && Time.time > nextFire)
So what is happening is the user clicks the left mouse button and if enough time has passed such that the current time is greater that the 'nextFire' time, then we can shoot again. Think about this... you see here:
nextFire = Time.time + fireRate;
So, depending on what you set 'fireRate' to determines the literal fire rate.
Example:
fireRate = 0.5f
will give you 2 shots every second.
fireRate = 0.1f
will give you 10 shots every second.
So what I would guess is happening is that when the user holds 'UP', it assigns a small value to 'fireRate' and the opposite for when the user holds 'DOWN'.
AnXgotta, first thanks for your answer. And let me tell you that I've been playing with the 'fire rate' changing it on the fly according to the up/down key pressed in an attemp to compesate the effect but it didn't work.
Besides, the 'fire rate' in the script, this in particular and many others I've seen, doesn't change at all. It's always the same. I guess it has something to do with the player and bullet speeds. I've made some tests sum$$anonymous$$g those speeds but didn't work either.
Is it possible to solve the issue? Thanks again!
Just to clarify... you do mean that the player shoots bullets faster or slower when moving up and down? Not that the bullet projectiles move faster or slower after being fired, correct?
Exactly, when moving up/down the bullets seems to travel faster/slower respectively even though the fire rate won't change.
For example, while pressing up and shooting the bullets look closer compared to when shooting only.
Again, 'AnXgotta' thanks for your patience.-
Ok. Your two sentences mean opposite things. We need to clarify again.
"when moving up/down the bullets seems to travel faster/slower respectively even though the fire rate won't change" = same fire rate, bullet projectile speed is variant.
"while pressing up and shooting the bullets look closer compared to when shooting only" = bullet projectile speed is same, fire rate is variant.
So is it: 1) Bullet projectiles speed is always the same, the fire rate is changing or 2) Bullet projectiles speed is changing, fire rate is always the same
I would load the project and look but I can't because I'm at work.
Ok, I'll try to clarify: I won't talk about speed or fire rate only about what I'd like to see on screen and that is "I'd like to keep the distance beetween bullets constant independently of pressing up/down while shooting".
And about loading the project right now there's no problem. I'll keep checking back until finding a solution.
Thank you 'AnXgotta'.-