This is the script that i use to move my 2d player object:
function UpdateMove(){
//X
if (IsPressingRight()){
currentSpeedX += acceleration * Time.deltaTime;
if (currentSpeedX > maxSpeed) currentSpeedX = maxSpeed;
}
else if (IsPressingLeft()){
currentSpeedX -= acceleration * Time.deltaTime;
if (currentSpeedX < -maxSpeed) currentSpeedX = -maxSpeed;
}
else {
if (currentSpeedX > 0){
currentSpeedX -= deceleration * Time.deltaTime;
if (currentSpeedX < 0) currentSpeedX = 0;
}
if (currentSpeedX < 0){
currentSpeedX += deceleration * Time.deltaTime;
if (currentSpeedX > 0) currentSpeedX = 0;
}
}
//Y
if (IsPressingUp()){
currentSpeedY += acceleration * Time.deltaTime;
if (currentSpeedY > maxSpeed) currentSpeedY = maxSpeed;
}
else if (IsPressingDown()){
currentSpeedY -= acceleration * Time.deltaTime;
if (currentSpeedY < -maxSpeed) currentSpeedY = -maxSpeed;
}
else {
if (currentSpeedY > 0){
currentSpeedY -= deceleration * Time.deltaTime;
if (currentSpeedY < 0) currentSpeedY = 0;
}
if (currentSpeedY < 0){
currentSpeedY += deceleration * Time.deltaTime;
if (currentSpeedY > 0) currentSpeedY = 0;
}
}
transform.Translate(currentSpeedX, currentSpeedY, 0);
}
This is using basic acceleration and deceleration physics to give it a smooth inertia effect, acceleration, deceleration adn maxSpeed are private final variables defined above, and the button checks are just maded with Input.getAxis()
As you can see, the script use time.deltaTime in order to make it frame indipendent, but it doesnt...
The speed is changing as the frame rate is different, if i put the game on lowest quality i get the game to run about 1700 FPS and it is moving smooth but if i tried to the highest settings (100 FPS) its become very slow
The function is putted inside Update()
How can i keep the same speed at different frame rates?
↧