73 lines
3.1 KiB
C#
73 lines
3.1 KiB
C#
using UnityEngine;
|
|
namespace Invector
|
|
{
|
|
public class vSpike : MonoBehaviour
|
|
{
|
|
HingeJoint joint;
|
|
[HideInInspector]
|
|
public vSpikeControl control;
|
|
|
|
void Start()
|
|
{
|
|
joint = GetComponent<HingeJoint>();
|
|
}
|
|
bool inConnect;
|
|
Transform impaled;
|
|
|
|
void OnCollisionEnter(Collision collision)
|
|
{
|
|
if (collision.rigidbody != null && collision.collider.GetComponent<vCharacterController.vDamageReceiver>() != null && !inConnect)
|
|
{
|
|
bool condition = control == null ? true : !control.attachColliders.Contains(collision.collider.transform);
|
|
if (control) control.attachColliders.Add(collision.collider.transform);
|
|
if (condition)
|
|
{
|
|
inConnect = true;
|
|
if (joint && collision.rigidbody)
|
|
joint.connectedBody = collision.rigidbody;
|
|
|
|
impaled = collision.transform;
|
|
foreach (Rigidbody body in collision.transform.root.GetComponentsInChildren<Rigidbody>())
|
|
{
|
|
if (!body.isKinematic)
|
|
{
|
|
body.linearVelocity = Vector3.zero;
|
|
}
|
|
}
|
|
var damageReceiver = collision.collider.GetComponent<vCharacterController.vDamageReceiver>();
|
|
if (damageReceiver && damageReceiver.ragdoll && damageReceiver.ragdoll.iChar != null)
|
|
{
|
|
var iChar = damageReceiver.ragdoll.iChar;
|
|
// Route the kill through TakeDamage instead of writing health directly: the
|
|
// ragdoll is only ever raised by the onActiveRagdoll event, which TakeDamage
|
|
// fires and ChangeHealth does not, so an impaled character used to just die
|
|
// standing up. Sent to the character rather than the receiver so the spike
|
|
// stays lethal regardless of that collider's damage multiplier.
|
|
var damage = new vDamage((int)iChar.currentHealth, true)
|
|
{
|
|
activeRagdoll = true,
|
|
ignoreDefense = true,
|
|
sender = transform,
|
|
receiver = iChar.transform,
|
|
hitPosition = collision.GetContact(0).point,
|
|
};
|
|
iChar.TakeDamage(damage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.transform != null && impaled != null && other.transform == impaled)
|
|
{
|
|
if (joint)
|
|
joint.connectedBody = null;
|
|
impaled = null;
|
|
if (control != null && control.attachColliders.Contains(impaled))
|
|
control.attachColliders.Remove(impaled);
|
|
inConnect = false;
|
|
}
|
|
}
|
|
}
|
|
} |