You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.6 KiB
C#

2 weeks ago
using UnityEngine;
namespace Fusion.Addons.SimpleKCC
{
internal sealed class KCCCollider
{
public GameObject GameObject;
public Transform Transform;
public CapsuleCollider Collider;
public bool IsSpawned;
public bool IsTrigger;
public float Radius;
public float Height;
public int Layer;
public void Update(KCC kcc)
{
KCCSettings settings = kcc.Settings;
if (!IsSpawned)
{
IsTrigger = settings.IsTrigger;
Radius = settings.Radius;
Height = settings.Height;
Layer = settings.ColliderLayer;
GameObject = new GameObject("KCCCollider");
GameObject.layer = settings.ColliderLayer;
Transform = GameObject.transform;
Transform.SetParent(kcc.Transform, worldPositionStays: false);
Transform.localPosition = Vector3.zero;
Transform.localRotation = Quaternion.identity;
Transform.localScale = Vector3.one;
Collider = GameObject.AddComponent<CapsuleCollider>();
Collider.direction = 1;
Collider.isTrigger = settings.IsTrigger;
Collider.radius = settings.Radius;
Collider.height = settings.Height;
Collider.center = new Vector3(0f, settings.Height * 0.5f, 0f);
IsSpawned = true;
}
if (IsTrigger != settings.IsTrigger)
{
IsTrigger = settings.IsTrigger;
Collider.isTrigger = settings.IsTrigger;
}
if (Radius != settings.Radius)
{
Radius = settings.Radius;
Collider.radius = settings.Radius;
}
if (Height != settings.Height)
{
Height = settings.Height;
Collider.height = settings.Height;
Collider.center = new Vector3(0f, settings.Height * 0.5f, 0f);
}
if (Layer != settings.ColliderLayer)
{
Layer = settings.ColliderLayer;
GameObject.layer = settings.ColliderLayer;
}
}
public void Update(KCCOffline kcc)
{
KCCSettings settings = kcc.Settings;
if (!IsSpawned)
{
IsTrigger = settings.IsTrigger;
Radius = settings.Radius;
Height = settings.Height;
Layer = settings.ColliderLayer;
GameObject = new GameObject("KCCCollider");
GameObject.layer = settings.ColliderLayer;
Transform = GameObject.transform;
Transform.SetParent(kcc.Transform, worldPositionStays: false);
Transform.localPosition = Vector3.zero;
Transform.localRotation = Quaternion.identity;
Transform.localScale = Vector3.one;
Collider = GameObject.AddComponent<CapsuleCollider>();
Collider.direction = 1;
Collider.isTrigger = settings.IsTrigger;
Collider.radius = settings.Radius;
Collider.height = settings.Height;
Collider.center = new Vector3(0f, settings.Height * 0.5f, 0f);
IsSpawned = true;
}
if (IsTrigger != settings.IsTrigger)
{
IsTrigger = settings.IsTrigger;
Collider.isTrigger = settings.IsTrigger;
}
if (Radius != settings.Radius)
{
Radius = settings.Radius;
Collider.radius = settings.Radius;
}
if (Height != settings.Height)
{
Height = settings.Height;
Collider.height = settings.Height;
Collider.center = new Vector3(0f, settings.Height * 0.5f, 0f);
}
if (Layer != settings.ColliderLayer)
{
Layer = settings.ColliderLayer;
GameObject.layer = settings.ColliderLayer;
}
}
public void Destroy()
{
if (IsSpawned)
{
if (Collider != null)
{
Collider.enabled = false;
}
Object.Destroy(GameObject);
GameObject = null;
Transform = null;
Collider = null;
IsSpawned = false;
IsTrigger = false;
Radius = 0f;
Height = 0f;
Layer = 0;
}
}
}
}