- Home /
Different calculations/math results between Unity editor and iPhone ?
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) )
tmpS$$anonymous$$lRate has already been multiplied by Time.deltaTime * 100, then you multiply again inside loseEnergy, which seems wrong ... but doesn't explain why results are different on the iphone.
yes thats probably bad thank you, i removed it but still, i don't get why it produces different results in iOS. I am afraid it has something to do with 'spikes' or sudden frameskips. Using fixedUpdate by the way, would be a bad idea for iPhone ?
Answer by user-11329 (google) · Apr 08, 2011 at 05:21 PM
I'm having the same problem with some very simple code. I'm updating a velocity value with acceleration*Time.deltaTime. In the editor preview it works as expected but on IPhone the velocity just increases to infinity. I'm still looking into the problem but it's a strange one.
after optimising more and more, my problem eventually disappeared. So, now i quite sure that it had to do with the low frame rate.
How many FPS are you getting? Is your average fps above a certain number, or it drops instantly now and then?
Your answer
Follow this Question
Related Questions
Low FPS rate. Can't find the problem. Followed Unity's Guide 2 Answers
How do I get high smooth FPS in Unity??(Improve Fillrate) 1 Answer
Is there a way to create applications that run on fixed frame rate of 60fps on iOS? 1 Answer
iPhone Resolution Screen Switch 0 Answers
Mini Mac - Can this Compile Unity? 2 Answers