I’m working on a Sci fy space simulator project right now and I was wondering how I can create rooms filled with air, I mean if that room is going to be damaged or destroyed the air is supposed to leak and then the room is no longer accessible. I was thinking about using triggers but triggering all rooms in the whole spaceship is a mess.
Is there any method that I can use to simulate air in my rooms?
Here is a super simple example, hope it helps.
using UnityEngine;
using System.Collections;
public class LeakingAir : MonoBehaviour {
public float airEscapeRate; // How quickly the air can escape.
public float percentOfAir; // How much air is in the room.
public bool roomIsAccessible; // Can the room be accessed?
public bool roomIsDamaged; // Is the room damaged?
float airLeakTimer; // The counter that removes air from the room.
float setAirLeakTimer = 100; // The amount of air in the room to begin with.
// At 100 it will register 100% full.
void Awake(){
roomIsAccessible = true; // To start the room can be accessed
roomIsDamaged = false; // and the room is not damaged
airLeakTimer = setAirLeakTimer; // Sets the timer. By setting the value in this way
// it would be possible to easily "fix" the room
// by resetting the airLeakTimer to the setAirLeakTimer.
}
void Update(){
percentOfAir = (airLeakTimer / 100) * 100; // Shows percentage of air in room.
if(roomIsDamaged) // When the room gets damaged
airLeakTimer -= Time.deltaTime * airEscapeRate; // the timer starts and is accelerated by airEscapeRate
if(airLeakTimer <= 0) // Makes sure the percentage doesn't do into the negative.
airLeakTimer = 0;
if(percentOfAir <= 0) // Checks how much air is in the room, if there is none
roomIsAccessible = false; // makes the room inaccessable.
}
}