- Home /
Objects moving slowly in preview.
I have a project with 5 scenes. (2d platformer side-scroller game)
In each scene, there are objects that move (controlled by a movement script). The speed at which they move is static. Nothing in my environment has changed over the course of game development.
Now for some reason, during preview, my objects move much much slower than before (SO SLOW) it's hard to 'test' my game so to speak. Only objects with a movement script attached (I have a few different movement scripts) are affected by this slow-down (these are things like moving platforms, enemies, props etc).
When I execute the game as a desktop app, objects move at the correct/normal movement speed.
What could be causing this slow-down in preview? Anyone have a clue? I would be happy to provide more detail.
private var Xpos : float;
private var Ypos : float;
private var max : boolean;
var Vert : boolean;
var maxAmount : int;
var step : float;
function Start () {
Xpos = transform.position.x;
Ypos = transform.position.y;
}
function Update () {
//set the max
if(Vert) { //verticle
if(transform.position.y >= Ypos + maxAmount){
max = true;
} else if(transform.position.y <= Ypos){
max = false;
}
} else { //horizontal
if(transform.position.x >= Xpos + maxAmount){
max = true;
} else if(transform.position.x <= Xpos){
max = false;
}
}
//moving the platform
if(Vert){ // Verticle movement
if(!max) {
transform.position.y += step;
} else {
transform.position.y -= step;
}
} else { //Horizontal movement
if(!max){
transform.position.x += step;
} else {
transform.position.x -= step;
}
}
}
Answer by tanoshimi · Feb 17, 2015 at 05:56 PM
You have written your movement scripts to be tied to the framerate of your game (which varies from machine to machine, and even from one moment to the next on a given machine).
If your computer runs at 60FPS, your object is moving "step" amount 60 times every second. If your computer slows down (perhaps your antivirus program starts a hard drive scan, or some other background process is running) and your framerate drops to 30FPS, your object will half its speed too.
You need to learn about Time.deltaTime to make your objects move the same speed, whatever the framerate at which your game is running.
Your answer
Follow this Question
Related Questions
< cheaper than <= ? 2 Answers
Movement speed of player is much slower in build than in the editor 3 Answers
Need help optimizing grid initialization. 1 Answer
How significant is the performance difference between a jagged array and a multidimensional array? 1 Answer
Wild fluctuations in performance when built into an exe... 1 Answer