wip on ragdoll

This commit is contained in:
in0finite 2021-01-04 01:28:58 +01:00
parent 64680f7280
commit d6d5d660cf
10 changed files with 55 additions and 7 deletions

View file

@ -171,6 +171,11 @@ MonoBehaviour:
AIOutOfRangeTimeout: 5
AIOutOfRangeDistance: 250
pedSyncRate: 20
ragdollMass: 100
ragdollLifetime: 90
ragdollDrag: 0.05
ragdollMaxDepenetrationVelocity: -1
ragdollDamageForce: 50
--- !u!114 &114560248511158306
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -25,6 +25,8 @@ namespace SanAndreasUnity.Behaviours {
[SerializeField] [Range(5, 100)] int m_defaultPhysicsUpdateRate = 30;
[SerializeField] [Range(5, 100)] int m_defaultPhysicsUpdateRateOnMobile = 20;
public static int DefaultLayerIndex => 0;
public Vector2 cursorSensitivity = new Vector2(2f, 2f);

View file

@ -210,7 +210,7 @@ namespace SanAndreasUnity.Behaviours
if (this.PlayerModel != null)
{
this.PlayerModel.DetachRagdoll();
this.PlayerModel.DetachRagdoll(this.KillingDamageInfo);
}
}

View file

@ -16,6 +16,11 @@ namespace SanAndreasUnity.Behaviours
public Bar HealthBar { get; private set; }
/// <summary>
/// Damage info that killed the ped.
/// </summary>
public DamageInfo KillingDamageInfo { get; set; }
void AwakeForDamage ()

View file

@ -318,6 +318,7 @@ namespace SanAndreasUnity.Behaviours.Peds.States
if (m_ped.Health <= 0)
{
m_ped.KillingDamageInfo = damageInfo;
Object.Destroy(m_ped.gameObject);
}

View file

@ -59,6 +59,9 @@ namespace SanAndreasUnity.Behaviours
public float ragdollMass = 100f;
public float ragdollLifetime = 30f;
public float ragdollDrag = 0.05f;
public float ragdollMaxDepenetrationVelocity = 10f;
public float ragdollDamageForce = 4f;
void Awake ()

View file

@ -382,12 +382,12 @@ namespace SanAndreasUnity.Behaviours
}
public Transform DetachRagdoll()
public Transform DetachRagdoll(DamageInfo damageInfo)
{
if (null == m_ragdollBuilder)
return null;
if (null == this.RootFrame)
if (null == this.UnnamedFrame)
return null;
m_ragdollBuilder.BuildBodies();
@ -396,18 +396,35 @@ namespace SanAndreasUnity.Behaviours
m_ragdollBuilder = null;
var ragdollTransform = this.RootFrame.transform;
var ragdollTransform = this.UnnamedFrame.transform;
ragdollTransform.SetParent(null);
// add velocity to ragdoll based on current ped's velocity
foreach (var rb in ragdollTransform.GetComponentsInChildren<Rigidbody>())
{
rb.drag = PedManager.Instance.ragdollDrag;
if (PedManager.Instance.ragdollMaxDepenetrationVelocity >= 0)
rb.maxDepenetrationVelocity = PedManager.Instance.ragdollMaxDepenetrationVelocity;
// add velocity to ragdoll based on current ped's velocity
rb.velocity = m_ped.Velocity;
}
// apply force to a collider that was hit by a weapon
if (damageInfo != null && damageInfo.raycastHitTransform != null && damageInfo.damageType == DamageType.Bullet)
{
var c = damageInfo.raycastHitTransform.GetComponent<Collider>();
if (c != null && c.attachedRigidbody != null)
{
c.attachedRigidbody.AddForceAtPosition(
damageInfo.hitDirection * damageInfo.amount.SqrtOrZero() * PedManager.Instance.ragdollDamageForce,
damageInfo.hitPoint,
ForceMode.Impulse);
}
}
// change layer
ragdollTransform.gameObject.SetLayerRecursive(0);
ragdollTransform.gameObject.SetLayerRecursive(GameManager.DefaultLayerIndex);

View file

@ -713,9 +713,11 @@ namespace SanAndreasUnity.Behaviours
{
amount = this.Damage,
raycastHitTransform = hit.collider.transform,
hitDirection = fireDir,
hitPoint = hit.point,
hitNormal = hit.normal,
attacker = m_ped,
damageType = DamageType.Bullet,
});
}

View file

@ -10,6 +10,7 @@ namespace SanAndreasUnity.Utilities
public float amount = 0f;
public string damageType = null;
public Transform raycastHitTransform = null;
public Vector3 hitDirection = Vector3.forward;
public Vector3 hitPoint = Vector3.zero;
public Vector3 hitNormal = Vector3.up;
public object attacker = null;
@ -20,7 +21,10 @@ namespace SanAndreasUnity.Utilities
{
public static readonly string
Bullet = "Bullet",
Explosion = "Explosion";
Explosion = "Explosion",
Gas = "Gas",
Flame = "Flame",
Melee = "Melee";
}
public class Damageable : MonoBehaviour

View file

@ -92,6 +92,15 @@ namespace SanAndreasUnity.Utilities
return Mathf.RoundToInt (f);
}
public static float SqrtOrZero(this float f)
{
if (float.IsNaN(f))
return 0f;
if (f <= 0f)
return 0f;
return Mathf.Sqrt(f);
}
public static double DateTimeToUnixTimestamp(this DateTime dateTime)
{
return (TimeZoneInfo.ConvertTimeToUtc(dateTime) -