I couldn't think of a way to put it better and also, i couldn't find any specific answers on the web so i am asking if some else has run into the same problem.
I am developing a game where the enemies absorb energy from the player. Depending on their distance the absorb rate varies from a min to max.
Now, I have this strange issue when testing the game: In the editor (or when building for Web/Mac) everything runs fine. The enemies absorb energy as expected.
When running the iPhone build though (in an iPhone 4G, iOS 4.2), most enemies completely drain the player's energy in less than a second (sometimes instantly!).
I assume that it has something to do with frame drops or frame-skips but i get 20-25 fps average (which isn't bad right?) Also, since calculating everything with * Time.deltaTime this shouldb't be an issue -theoretically-.
I even tried locking the framerate but to no result.
The code I'm using for the above is as follows: The distance between an enemy and the player is calculated by the enemy inside the Update function using:
function Update(){
distance = Vector3.Distance(playerObject.transform.position, transform.position);
//where playerObject is obviously the player.
//some other code here...
//Then the script calls the absorb function
if ( distance < minDistance ){
Absorb();
}
private function Absorb(){
if ( ! dead ){
//Charge
if ( energy < energyNeeded){
var tmpStealRate:float = (stealRate / (distance/2)) * Time.deltaTime * 100 ;
energy += tmpStealRate;
//Attract particles
playerParticlesHolder.LookAt(transform);
player.loseEnergy(tmpStealRate);
}
}
}
The above code is in the same script. The loseEnergy function in the player script is as follows
public function loseEnergy(lossRate: float){
if ( Time.timeScale != 0 ){
if ( energy >= 0.0 ){
energy -= lossRate * Time.deltaTime * 100 ;
updateParticles();
draining = true;
}
else {
GetComponentInChildren.<ParticleEmitter>().emit = false;
GameOver();
}
}
}
EVERY script in the game uses Update function. There is not one single FixedUpdate in the game.
Also, using the profiler in unity and the xcode instruments, i see no more than 15-20% CPU usage which is way too low to assume that there are continues frame-skips/drops.
(btw, I use deltaTime * 100 because it helps me using float values in the inspector. i.e. the stealRate on most enemies is 0.3 ( which should be 30 per second ingame) )