Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. protected virtual void OnCollisionEnter(Collision collision)
  2.     {
  3.         if (collision.impulse == Vector3.zero)
  4.             return;
  5.  
  6.         Vector3 previousAvgVelocity = avgVelocity;
  7.  
  8.         if (printCollision && isDamaging && Time.time - lastDamageTime > 0.15f)
  9.         {
  10.             VRLog.Log(name + " Collision with " + collision.rigidbody.gameObject.name + " relative velocity " + collision.relativeVelocity + " ( " + collision.relativeVelocity.magnitude + " )  tracked velocity " + trackedVelocity + " ( " + trackedVelocity.magnitude + " ) rigidbody velocity " + GetComponent<Rigidbody>().velocity + " ( " + GetComponent<Rigidbody>().velocity.magnitude + " )  average velocity " + avgVelocity + " ( " + avgVelocity.magnitude + ")");
  11.         }
  12.  
  13.         if (minimumDamageVelocity > 0f)
  14.         {
  15.  
  16.             if (avgVelocity.magnitude < minimumDamageVelocity)
  17.                 return;
  18.         }
  19.         if (weaponBase != null && weaponBase.grabbedByHand != null)
  20.         {
  21.             weaponBase.grabbedByHand.HitSomething(collision.relativeVelocity - velocity, collision.gameObject);
  22.         }
  23.         else if (GetComponentInParent<Fist>() != null)
  24.         {
  25.             GetComponentInParent<Fist>().VibrateControllerPulse((collision.relativeVelocity - velocity).magnitude * 0.05f, 0.4f);
  26.         }
  27.         if (collision.rigidbody != null && isDamaging)
  28.         {
  29.             var db = collision.rigidbody.GetComponent<DamageRelay>();
  30.             if (isPlayerFist && db != null && db.GetComponent<SeverableLimb>() != null && db.GetComponent<SeverableLimb>().armorPiece != null)
  31.             {
  32.                 db = db.GetComponent<SeverableLimb>().armorPiece;
  33.             }
  34.             Vector3 relativeVel = collision.relativeVelocity;
  35.             if (addKinematicVelocity)
  36.             {
  37.                 relativeVel -= velocity;
  38.             }
  39.             if (avgVelocity.y > 0f && damageType != DamageType.Stab && damageType != DamageType.Cut)
  40.             {
  41.                
  42.                 avgVelocity.y *= 1.5f;
  43.                 if (isHeavy)
  44.                     avgVelocity.y *= 2f;
  45.             }
  46.             if (!IsThisGameObjectMyWielder(db))
  47.             {
  48.                 if (db != null)
  49.                 {
  50.  
  51.  
  52.                     if (db != null && Time.time - lastDamageTime > 0.15f)
  53.                     {
  54.                         lastDamageTime = Time.time;
  55.                         Vector3 damage = scaleDamage * relativeVel;
  56.                         Vector3 hitNormal = Vector3.Cross(transform.forward, avgVelocity);
  57.                         if (db.GetComponent<PlayerDamageRelay>() == null)
  58.                         {
  59.                             if (damageType == DamageType.Stab && !db.IsArmorPiece)
  60.                             {
  61.                                 if (Vector3.Dot(avgVelocity.normalized, transform.forward) > 0.5f)
  62.                                 {
  63.  
  64.                                     Impale(db, collision.contacts[0].point);
  65.                                 }
  66.                                 else
  67.                                 {
  68.                                     VRLog.Log("averagevelocity not correct: " + avgVelocity.normalized + ", " + transform.forward);
  69.                                     return;
  70.                                 }
  71.                             }
  72.  
  73.  
  74.                             if (bonusVelocity != 0f)
  75.                             {
  76.                                 var totalBonusVelocity = bonusVelocity + (IsWieldedByPlayer() ? playerWieldedBonusVelocity : 0f);
  77.                                 //print("Relative vel: " + relativeVel + ", avg: " + avgVelocity + ", " + collision.rigidbody.gameObject.name);
  78.                                 if (db.IsArmorPiece)
  79.                                 {
  80.                                     if (!db.GetComponent<ArmorPiece>().detached)
  81.                                     {
  82.  
  83.  
  84.                                         db.GetComponent<ArmorPiece>().armoredTo.rigidbody.AddForceAtPosition(-relativeVel * totalBonusVelocity * 0.5f, collision.contacts[0].point, ForceMode.VelocityChange);
  85.  
  86.                                         if (isHeavy)
  87.                                         {
  88.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.chest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  89.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.lowerChest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  90.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.pelvis.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  91.  
  92.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.Damage(damage.magnitude / 10f, 0f, this.gameObject, damageType);
  93.                                         }
  94.                                         else
  95.                                         {
  96.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.chest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  97.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.lowerChest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  98.                                             db.GetComponent<ArmorPiece>().armoredTo.owner.pelvis.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.25f, collision.contacts[0].point, ForceMode.VelocityChange);
  99.                                         }
  100.  
  101.                                     }
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                     collision.rigidbody.AddForceAtPosition(-relativeVel * totalBonusVelocity * 0.5f, collision.contacts[0].point, ForceMode.VelocityChange);
  106.                                     if ((weaponBase != null || isPlayerFist))
  107.                                     {
  108.                                         Vector3 actualVel = GetComponent<Rigidbody>().velocity + velocity;
  109.  
  110.                                         db.GetComponent<SeverableLimb>().owner.chest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.35f, collision.contacts[0].point, ForceMode.VelocityChange);
  111.                                         db.GetComponent<SeverableLimb>().owner.lowerChest.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.35f, collision.contacts[0].point, ForceMode.VelocityChange);
  112.                                         db.GetComponent<SeverableLimb>().owner.pelvis.GetComponent<Rigidbody>().AddForceAtPosition(avgVelocity * totalBonusVelocity * 0.35f, collision.contacts[0].point, ForceMode.VelocityChange);
  113.                                     }
  114.                                 }
  115.                             }
  116.                         }
  117.                         DoDamage();
  118.                         db.Damage(damageType, damage, collision.contacts[0].point, this.gameObject, hitNormal, collision);
  119.                     }
  120.                 }
  121.                 else if (collision.rigidbody.GetComponent<WeaponImpactSound>() != null)
  122.                 {
  123.                     if (collision.rigidbody.GetComponent<DamagerRigidbody>() != null && !HasSameWielder(collision.rigidbody.GetComponent<DamagerRigidbody>()) && Time.time - lastDamageTime > 0.15f)
  124.                     {
  125.                         lastDamageTime = Time.time + 0.1f;
  126.                         if (GetComponent<Rigidbody>() != null)
  127.                         {
  128.                             GetComponent<Rigidbody>().AddForce(relativeVel * 0.9f, ForceMode.VelocityChange);
  129.                         }
  130.                         if (collision.rigidbody.GetComponent<DamagerRigidbody>().weaponBase != null)
  131.                         {
  132.                             bool byPlayer = false;
  133.                             if (isPlayerFist)
  134.                                 byPlayer = true;
  135.                             else if (weaponBase != null && weaponBase.wieldedByPlayer)
  136.                                 byPlayer = true;
  137.                             if (byPlayer)
  138.                                 collision.rigidbody.GetComponent<DamagerRigidbody>().weaponBase.RecoilArm(-relativeVel * 0.75f, collision.contacts[0].point, byPlayer, this);
  139.                         }
  140.  
  141.                         if (collision.rigidbody.GetComponent<DamagableShield>() != null)
  142.                         {
  143.                             //print(name + " damaging shield");
  144.                             collision.rigidbody.GetComponent<DamagableShield>().Damage(collision, velocity, scaleDamage * relativeVel, damageType, isHeavy);
  145.                         }
  146.  
  147.                     }
  148.                 }
  149.                 else if (GetComponent<FisticuffFist>() != null && collision.rigidbody.GetComponent<DamagerRigidbody>() != null && collision.rigidbody.GetComponent<DamagerRigidbody>().isPlayerFist)
  150.                 {
  151.                     GetComponent<FisticuffFist>().owner.AI.InterruptAttack();
  152.                 }
  153.             }
  154.  
  155.         }
  156.         avgVelocity = previousAvgVelocity;
  157.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement