Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075919 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:11:17 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Unity Game Question
Pages: [1]
Print
Author Topic: Unity Game Question  (Read 989 times)
tbishop9870
Level 0
*

tbishop9870
View Profile WWW Email
« on: April 19, 2011, 05:39:28 PM »

Ok, so I am kind of new to this forum when it comes to posting, but I figured if there was any place to ask this question it was here.

I am currently building just a small easy game in unity that deals with a player shooting enemies that are flying at them. I have the Enemies spawning on a time basis (every three seconds) with an if statement in a FixedUpdate() function:

Code:
If (Time.timeSinceLevelLoad % 3 == 0){
                  var position = Vector3(Random.Range(-5, 5), .5, 80);
      Instantiate(prefab, position, Quaternion.identity);
              }

When I run the scene that the enemies spawn in everything works fine, they spawn everything three seconds with no problem. But when I run the game from other scenes that load that level, everything works except for that if statement. I have run a series of debugs and determined that the FixedUpdate() function is getting called, but the if statement is not being run. I altered the code again in an attempt to debug and found out that the problem lies in the "(Time.timeSinceLevelLoad % 3 == 0)" part. If I change the == to a >= it runs, and if I change it to != it runs. The only times it does not run is when I have it at == and that is only when this level is loaded from another scene. I am completely stuck now so any help would be greatly appreciated. Sad
Logged

Gimym JIMBERT
Level 10
*****


NOTGAMER ludophile


View Profile Email
« Reply #1 on: April 19, 2011, 05:44:25 PM »

Maybe you should made a custom counter that initialize itself when the script is being instantiate?
Logged


ILLOGICAL, random guy on internet, do not trust (lelebĉcülo dum borobürükiss) ! GЮЯЦ TФ ДЯSTӨTZҚД!
sonic the heidegger (Überall Geschwindigkeit)
TheLastBanana
Level 8
***



View Profile WWW Email
« Reply #2 on: April 19, 2011, 06:27:37 PM »

Part of your issue probably lies in the fact that Time.timeSinceLevelLoad is a float, meaning it can have decimal places. Since you're calling this in FixedUpdate, which is called in every fixed update frame, you're not always going to get a rounded number. More than likely, Time.timeSinceLevelLoad % 3 is working out to something like 0.005, meaning it isn't exactly 0. As well, comparison between floats using the equal operator isn't very accurate - you should always use Mathf.Approximately for that.

One thing you can give a try for this is a coroutine. They're very useful in Unity, and they fit what you're trying to do quite well. Just set up a function that looks like this:
Code:
function SpawnEnemies() {
while (true) {
yield WaitForSeconds(3);

var position = Vector3(Random.Range(-5, 5), .5, 80);
Instantiate(prefab, position, Quaternion.identity);
}
}
This code waits for 3 seconds, then spawns an enemy using your code, and just repeats that indefinitely. "yield" lets other scripts run while this function stays as a coroutine, meaning that the infinite loop won't freeze your game - it'll just keep spawning an enemy every 3 seconds. To start the function, just call it once in a script's Start function like so:
Code:
function Start() {
SpawnEnemies();
//Or, if you're using C#, call this:
//StartCoroutine(SpawnEnemies());
}

And that's it! Hope that helps. Let me know if any of this confuses you and I'll try to help. Smiley
Logged

tbishop9870
Level 0
*

tbishop9870
View Profile WWW Email
« Reply #3 on: April 19, 2011, 09:14:02 PM »

Thanks for the quick response guys! Banana: I will most likely implement that code, so thank you very much for your assistance!
Logged

Matthew
Rapture
Administrator
Level 3
******


Milling About


View Profile WWW
« Reply #4 on: April 21, 2011, 11:21:03 AM »

Could also use InvokeRepeating.
Logged

Matthew Wegner
Currently: Aztez
Founder, Flashbang Studios
Partner, Indie Fund
Editor, Fun-Motion
Co-Chair, IGF
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic