Unity combat - simple fire
This time I felt the particle system of unity, and designed a simple fire, and controlled the shape of the fire in different situations through code.
final effect
simple stove
making flames
-
Create an empty object, name it file , add components
Particle System
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-
-
Modify
Emission
the propertyRate over Time
to let the system release more particles at the same time
-
Shape
Modify the shape in the propertyBox
, modify the x directionRotation
to -90 , and make the particles float upward
-
In order to make the particle system more flame-like, check
Size over LifeTime
the attribute and edit the waveform to make it closer to the shape of the flame
- In order to make the color closer to the flame, check it
Color over LifeTime
and adjust the color
-
Adjust the basic parameters to make the visual effect better
Fire
Drag intoAssets
as a prefab
script writing
Distinguish between small fire, medium fire and high fire from two aspects
- number of flames
- Flame size and shape
Write a script to adjust the flame according to the above criteria
The structure of this script is relatively simple, which is a simplification of the classic mvc structure
FirstController.cs
: The main controller of the program, responsible for loading resources, controlling and recycling flames
The method in the script is introduced below
Start
: load resources, initialize data
void Start()
{
fireList = new List<GameObject>();
userGUI = gameObject.AddComponent<UserGUI>();
status = userGUI.GetStatus();
}
Update
: Monitor status
the property , and call the response function to control the change of the flame
void Update()
{
int oldStatus = status;
status = userGUI.GetStatus();
if(oldStatus != status){
if(oldStatus > 0 && status == 0){
OffFire();
}
else{
if(status == 1){
SmallFire();
}
if(status == 2){
MiddleFire();
}
if(status == 3){
LargeFire();
}
}
}
}
SmallFire
, MiddleFire
, LargeFire
: Load and adjust the properties of the particle system
A total of 5 flames are loaded on low fire, 7 on medium fire, and 9 on high fire. The corresponding scale, rateOverTime, and startLifetime are also modified by code, as a distinction
public void SmallFire(){
DestroyFire();
for(int i = 0; i < 5; i++){
fire = Instantiate(Resources.Load("Prefabs/Fire"),
new Vector3(small_pos[i], 0f, z),
Quaternion.identity)
as GameObject;
fireList.Add(fire);
ParticleSystem ps = fire.GetComponent<ParticleSystem>();
var sh = ps.shape;
sh.radius = 0.69f;
sh.scale = new Vector3(0.1f, 0.1f, 0);
var em = ps.emission;
em.rateOverTime = 300f;
}
}
public void MiddleFire(){
DestroyFire();
for(int i = 0; i < 7; i++){
fire = Instantiate(Resources.Load("Prefabs/Fire"),
new Vector3(middle_pos[i], 0f, z),
Quaternion.identity)
as GameObject;
fireList.Add(fire);
ParticleSystem ps = fire.GetComponent<ParticleSystem>();
var sh = ps.shape;
sh.radius = 0.69f;
sh.scale = new Vector3(0.5f, 0.5f, 0);
var em = ps.emission;
em.rateOverTime = 500f;
}
}
public void LargeFire(){
DestroyFire();
for(int i = 0; i < 9; i++){
fire = Instantiate(Resources.Load("Prefabs/Fire"),
new Vector3(large_pos[i], 0f, z),
Quaternion.identity)
as GameObject;
fireList.Add(fire);
ParticleSystem ps = fire.GetComponent<ParticleSystem>();
var sh = ps.shape;
sh.radius = 0.69f;
sh.scale = new Vector3(1f, 1f, 0);
var em = ps.emission;
em.rateOverTime = 700f;
var main = ps.main;
main.startLifetime = 1.8f;
}
}
OffFire
: Extinguish the flames by changing the loop property of the particle system to False
public void OffFire(){
foreach(GameObject fire in fireList){
ParticleSystem ps = fire.GetComponent<ParticleSystem>();
var main = ps.main;
main.loop = false;
}
}
DestroyFire
: Destroy the flames in the list
public void DestroyFire(){
foreach(GameObject fire in fireList){
fire.SetActive(false);
}
fireList.Clear();
}
UserGUI.cs
: The user's main interface
public class UserGUI : MonoBehaviour
{
private FirstController firstController;
GUIStyle textStyle;
GUIStyle topicStyle;
int status;
// Start is called before the first frame update
void Start()
{
SetStyle();
status = 0;
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
}
void SetStyle(){
textStyle = new GUIStyle();
textStyle.normal.textColor = Color.black;
textStyle.fontSize = 30;
topicStyle = new GUIStyle();
topicStyle.normal.textColor = Color.black;
topicStyle.fontSize = 60;
}
void OnGUI(){
if (GUI.Button(new Rect(Screen.width/2 - 300, Screen.width/2 - 300, 60, 50), "熄灭"))
{
status = 0;
}
if (GUI.Button(new Rect(Screen.width/2 - 300, Screen.width/2 - 240, 60, 50), "小火"))
{
status = 1;
}
if (GUI.Button(new Rect(Screen.width/2 - 300, Screen.width/2 - 180, 60, 50), "中火"))
{
status = 2;
}
if (GUI.Button(new Rect(Screen.width/2 - 300, Screen.width/2 - 120, 60, 50), "大火"))
{
status = 3;
}
}
public int GetStatus(){
return status;
}
}
Reference Resources
Blog
Unity Manual – Particle System