- Home /
problem with bullet instantiation
I have a mechanic in my game that allows the player to slow down time but whenever I use it, my bullet spawner starts spitting out bullets at a higher rate. What do I do? Here's my bullet spawner script:
#pragma strict
var projectile : GameObject;
var timeUntilDestroyed : float;
var timeBetweenBulletsCurrent : float;
var timeBetweenBullets1 : float;
var timeBetweenBullets2 : float;
var patternChangeTime : float;
var patternChangeTime2 : float;
var isFiring : boolean;
var isChanging : boolean;
function ChangeSpeedEvery() {
isChanging = true;
timeBetweenBulletsCurrent = timeBetweenBullets1;
yield WaitForSeconds(patternChangeTime);
timeBetweenBulletsCurrent = timeBetweenBullets2;
yield WaitForSeconds(patternChangeTime2);
isChanging = false;
}
function FireBullet() {
var clone = Instantiate(projectile, transform.position, Quaternion.identity);
Destroy(clone, timeUntilDestroyed);
}
function FireBulletRepeatedly() {
isFiring = true;
FireBullet();
yield WaitForSeconds(timeBetweenBulletsCurrent);
isFiring = false;
}
function FixedUpdate() {
if(isFiring == false) {
FireBulletRepeatedly();
}
if(isChanging == false) {
ChangeSpeedEvery();
}
}
So how are you achieving the time slowdown effect, an obvious solution would be to multiply the floats in wait for seconds with the timescale
I'm halving the time scale and delta time. I've tried multiplying all the waitforseconds by deltaTime and that didn't work. It still spits out bullets like twice as fast.
Here's the code that slows down time: #pragma strict
var playerObj : GameObject;
var audioSrc : AudioSource;
var sta$$anonymous$$aCost : float;
var slowDownFactor : float;
var originalScale : float;
var targetScale : float;
var speed : float;
var slowDownLength : float;
var timeIsSlower : boolean;
private var thisBool : boolean;
private var isActive : boolean;
function HalfTarget() {
if(timeIsSlower == true) {
targetScale = slowDownFactor;
}
else {
targetScale = originalScale;
}
}
function RestoreTarget() {
if(timeIsSlower == false) {
targetScale = originalScale;
timeIsSlower = false;
isActive = false;
}
else {
targetScale = slowDownFactor;
}
}
function SlowDownMusic() {
audioSrc.pitch = 0.5;
}
function ReturnMusic() {
audioSrc.pitch = 1;
}
function SlowDownTime() {
HalfTarget();
SlowDownMusic();
isActive = true;
playerObj.GetComponent(playerSta$$anonymous$$a1).currentSta$$anonymous$$a -= sta$$anonymous$$aCost;
yield WaitForSeconds(slowDownLength);
timeIsSlower = false;
RestoreTarget();
ReturnMusic();
isActive = false;
thisBool = false;
}
function Start() {
audioSrc = GameObject.FindWithTag("Music").GetComponent.<AudioSource>();
}
function Update () {
if(Input.GetKeyDown(KeyCode.P)) {
if(isActive == false && playerObj.GetComponent(playerSta$$anonymous$$a1).currentSta$$anonymous$$a >= sta$$anonymous$$aCost) {
timeIsSlower = true;
thisBool = true;
}
else if(isActive == false && playerObj.GetComponent(playerSta$$anonymous$$a1).currentSta$$anonymous$$a < sta$$anonymous$$aCost) {
timeIsSlower = false;
thisBool = false;
}
else {
timeIsSlower = false;
thisBool = false;
}
}
if(timeIsSlower == true && thisBool == true && playerObj.GetComponent(playerSta$$anonymous$$a1).currentSta$$anonymous$$a >= sta$$anonymous$$aCost) {
SlowDownTime();
SlowDownMusic();
thisBool = false;
}
if(Input.GetKeyDown(KeyCode.P) && isActive == true) {
RestoreTarget();
thisBool = false;
}
if(isActive == false && Input.GetKeyDown(KeyCode.P)) {
ReturnMusic();
}
Time.timeScale = Mathf.MoveTowards(Time.timeScale, targetScale, speed * Time.deltaTime);
}
don't multiply It with Time.deltaTime, instead divide it by Time.timeScale....for example let's take a float 2....if you halve the timeScale 2/timeScale will become twice as much aka 4...so in all your waitforseconds statements divide the Time by Time.timeScale....and that should do it
Answer by BrainUK · Aug 11, 2021 at 09:35 AM
I multiplied one of the values by Time.time and this seems to have fixed it. Here's my code:
#pragma strict
var projectile : GameObject;
var timeUntilDestroyed : float;
var timeBetweenBulletsCurrent : float;
var timeBetweenBullets1 : float;
var timeBetweenBullets2 : float;
var patternChangeTime : float;
var patternChangeTime2 : float;
var isFiring : boolean;
var isChanging : boolean;
function ChangeSpeedEvery() {
isChanging = true;
timeBetweenBulletsCurrent = timeBetweenBullets1;
yield WaitForSeconds(patternChangeTime);
timeBetweenBulletsCurrent = timeBetweenBullets2;
yield WaitForSeconds(patternChangeTime2);
isChanging = false;
}
function FireBullet() {
var clone = Instantiate(projectile, transform.position, Quaternion.identity);
Destroy(clone, timeUntilDestroyed);
}
function FireBulletRepeatedly() {
isFiring = true;
FireBullet();
yield WaitForSeconds(timeBetweenBulletsCurrent * Time.time);
isFiring = false;
}
function Update() {
if(isFiring == false) {
FireBulletRepeatedly();
}
if(isChanging == false) {
ChangeSpeedEvery();
}
}
im not sure if time to time is good enough in the long run, for example if the game is open for many hours, it might take hours to get the gun to fire again, it feels fine immediately when you first load it up, i would still recommend dividing it by Time.timeScale
Thanks for the advice. I'll try dividing it by timeScale and see what happens
I tried dividing by Time.timeScale and the problem came back so I'm gonna have to stick with Time.time I think until I figure out a better way :/
Hmm i see...well this is a strange problem....well all the best..ill try and see what the problem is...if i find a good solution i will update you....
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
How to find triangles using variables 1 Answer
function OnCollosionEnter problems 1 Answer
Assign Default For Large Array 1 Answer