BloodAndGore/Assets/Scripts/Effects/BloodParticle.cs

53 lines
2.2 KiB
C#
Raw Normal View History

2019-08-03 18:14:47 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon {
public class BloodParticle : MonoBehaviour {
public Vector2 LaunchForce = new Vector2();
public bool Settled = false;
private Rigidbody2D CurrentlyStuckOn = null;
private Vector2 Velocity;
private bool GetsStuck;
private void Start() {
transform.localScale = new Vector2(0.02f, 0.02f) + Random.value * new Vector2(0.10f, 0.10f);
Velocity = LaunchForce + new Vector2(Random.value - 0.5f, Random.value - 0.5f);
GetsStuck = Random.value > 0.5;
}
private void FixedUpdate() {
Velocity *= 1.0f - Time.fixedDeltaTime * 20.0f;
if (CurrentlyStuckOn != null) {
bool IsStatic = CurrentlyStuckOn.bodyType == RigidbodyType2D.Static;
2019-08-04 00:48:41 +02:00
Velocity *= 1.0f - Time.fixedDeltaTime * (IsStatic ? 50.0f : 15.0f);
2019-08-03 18:14:47 +02:00
}
if (Velocity.magnitude < 0.1) {
if (CurrentlyStuckOn != null && GetsStuck) {
transform.parent = CurrentlyStuckOn.transform;
2019-08-04 00:48:41 +02:00
GetComponentInChildren<SpriteRenderer>().sortingLayerName = "Blood Particles (On characters)";
2019-08-03 18:14:47 +02:00
}
Destroy(GetComponent<BoxCollider2D>());
Destroy(this);
}
Vector2 AppliedMovement = Velocity * Time.fixedDeltaTime;
RaycastHit2D Hit = Physics2D.Raycast(transform.position, AppliedMovement, AppliedMovement.magnitude);
if (Hit.rigidbody != null && Hit.rigidbody.gameObject == Hit.collider.gameObject) {
2019-08-03 18:14:47 +02:00
bool IsStatic = Hit.rigidbody.bodyType == RigidbodyType2D.Static;
Velocity = Velocity.normalized * Mathf.Min(IsStatic ? 15.0f : 60.0f, Velocity.magnitude);
CurrentlyStuckOn = Hit.rigidbody;
AppliedMovement = Velocity * Time.fixedDeltaTime;
2019-08-04 00:48:41 +02:00
} else if (CurrentlyStuckOn != null) {
CurrentlyStuckOn = null;
2019-08-03 18:14:47 +02:00
}
Vector2 NewPosition = (Vector2)transform.position + AppliedMovement;
transform.position = NewPosition;
}
}
}