- Home /
How to create air in the rooms
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?
Just write the code for it, using Time.deltaTime to gradually decrease your air variable.
But how can I know if this room has air in it or not?
by making air into a variable in a script and making the room inacessable if the air variable is = to 0
you can learn how to do this by going to this link and watching the tutorials and if that's not enough tutorials then youtube also has some excellent tutorials for scriptinglink text
Answer by Spherical Cow · Dec 09, 2013 at 01:47 PM
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.
}
}
Your answer
Follow this Question
Related Questions
AirPlane Script? 2 Answers
How can I create an atmosphere/oxygen system in my 3d space game. 1 Answer
Air volume and flow between rooms like FTL 2 Answers
Addforce lasting 1 frame 1 Answer