Частицы воды иногда проходят через линию, и они не должны этого делать.
Вот изображение для справки: https://ibb.co/hL8RjT4
Как видите, пузырьки воды проходят через ту линию, которая должна удерживать воду.
Есть идеи, как это исправить?
Это скриптГенератор частиц:
using UnityEngine;
using System.Collections;
public class ParticleGenerator : MonoBehaviour {
public float WaterSpawnTimeInterval = 0.01f; //How much time until the next particle spawns
public float TapClosingTime = 2.5f; //How much time iy will take to close the tap
float lastSpawnTime = float.MinValue; //The last spawn time
private Vector3 particleForce; //Is there a initial force particles should have?
private Transform particlesParent; //Where will the spawned particles will be parented (To avoid covering the whole inspector with them)
void Start() {
Invoke("TapClose", time: TapClosingTime);
GetComponent<AudioSource>().Play();
particlesParent = this.transform;
if (transform.eulerAngles == Vector3.zero)
{
particleForce = Vector3.zero;
}
if (transform.eulerAngles.z == 90)
{
particleForce = new Vector3(90,0,0);
}
if (transform.eulerAngles.z == 270)
{
particleForce = new Vector3(-90, 0, 0);
}
}
void FixedUpdate()
{
if (lastSpawnTime + WaterSpawnTimeInterval < Time.time) // Is it time already for spawning a new particle?
{
GameObject newLiquidParticle = (GameObject)Instantiate(Resources.Load("DynamicParticle")); //Spawn a particle Dont remove the particle from resource folder
newLiquidParticle.GetComponent<Rigidbody2D>().AddForce(particleForce); //Add our custom force
DynamicParticle particleScript = newLiquidParticle.GetComponent<DynamicParticle>(); // Get the particle script
newLiquidParticle.transform.position = new Vector3(Random.Range(transform.position.x - .001f,
transform.position.x + .001f), Random.Range(transform.position.y - .001f, max: transform.position.y
+ .001f), 0); // Relocate to the spawner position
newLiquidParticle.transform.parent = particlesParent; // Add the particle to the parent container
lastSpawnTime = Time.time; // Register the last spawnTime
}
}
void TapClose()
{
GetComponent<AudioSource>().Stop();
GetComponent<ParticleGenerator>().enabled = false;
}
}
А вот сценарий DynamicParticle:
using UnityEngine;
using System.Collections;
public class DynamicParticle : MonoBehaviour {
public float GasFlotability = -.5f; //How fast does the gas goes up
public float particleLifeTime = 1f; //How much time before the particle scalesdown and dies
float startTime;
float scaleValue;
bool gas;
void FixedUpdate()
{
ScaleDown();
}
// The effect for the gas particle to seem to fade away
void ScaleDown()
{
if (gas)
{
scaleValue = 1.0f - ((Time.time - startTime) / particleLifeTime);
Vector2 particleScale = Vector2.one;
if (scaleValue <= 0)
{
Destroy(gameObject);
}
else
{
particleScale.x = scaleValue;
particleScale.y = scaleValue;
transform.localScale = particleScale;
}
}
}
void OnBecameInvisible()
{
if (FindObjectOfType<GameManager>())
{
FindObjectOfType<GameManager>().WaterCheck();
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.tag == "Hot")
{
startTime = Time.time;
gas = true;
Destroy(GetComponent<TrailRenderer>());
GetComponent<SpriteRenderer>().color = new Color(.7f, .7f, .7f, 1);
GetComponent<Rigidbody2D>().velocity = Vector3.zero;
GetComponent<Rigidbody2D>().gravityScale = GasFlotability;
GetComponent<Collider2D>().enabled = false;
GetComponent<SpriteRenderer>().sortingOrder = 3;
}
}
}