SHARE
TWEET

Puppet Master Body Collision Integration with Invector TPC

flow____ Dec 2nd, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Invector.vCharacterController;
  2. using RootMotion.Dynamics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. namespace Fox.Flow {
  8.     public class vThirdPersonControllerPuppetMaster : vThirdPersonController {
  9.  
  10.  
  11.         //##########################################################################################
  12.         //##########################################################################################
  13.         //
  14.         //                                DAMAGE
  15.         //
  16.         //##########################################################################################
  17.         //##########################################################################################
  18.  
  19.         protected PuppetMaster puppetMaster;
  20.         protected BehaviourPuppet behaviourPuppet;
  21.  
  22.         private List<BodyCollisionInfo> BodyCollision = new List<BodyCollisionInfo>();
  23.         public struct BodyCollisionInfo
  24.         {
  25.             public int muscle;
  26.             public Vector3 position;
  27.             public BodyCollisionInfo( int m, Vector3 p )
  28.             {
  29.                 muscle = m;
  30.                 position = p;
  31.             }
  32.         }
  33.  
  34.         protected override void Start()
  35.         {
  36.             base.Start();
  37.  
  38.             // GETTING THE COMPONENTS ( SHOULD WORK WHEN PUPPET MASTER 0.9 IS SET UP CORRECTLY )
  39.             puppetMaster = this.transform.parent.GetComponentInChildren<PuppetMaster>();
  40.             behaviourPuppet = puppetMaster.behaviours.ToList().Find( x => x is BehaviourPuppet ) as BehaviourPuppet;
  41.  
  42.             // ADDING THE MUSCLE COLLISION EVENT
  43.             behaviourPuppet.OnPreMuscleCollision += OnMuscleCollision;
  44.         }
  45.  
  46.  
  47.         private void OnMuscleCollision( MuscleCollision m )
  48.         {
  49.             // DON'T APPLY DAMAGE TO PROPS
  50.             if ( puppetMaster.muscles[ m.muscleIndex ].props.group == Muscle.Group.Prop ) return;
  51.             // RETURN WHEN DEAD
  52.             if ( isDead ) return;
  53.  
  54.             // TO PREVENT THE CHARACTER TAKING DAMAGE ON THE COLLIDER
  55.             // WHEN JUST STANDING IN PLACE OR BY JUST TOUCHING THE OTHER
  56.             // COLLIDER I IMPLEMENTED A DISTANCE CHECK ON THE HIT POINT
  57.             // TO PREVENT THIS SITUATION:
  58.             // FIRST GETTING THE LAST HIT
  59.             var bodyPart = GetCollisionInfo( m.muscleIndex );
  60.             // GETTING THE CURRENT HIT
  61.             BodyCollisionInfo newBodyPart = new BodyCollisionInfo( m.muscleIndex, m.collision.contacts[ 0 ].point );
  62.             BodyCollision.Add( newBodyPart );
  63.             bool count = false;
  64.             // WHEN THERE IS NO LAST HIT THAN COUNT
  65.             if ( bodyPart.Equals( default( BodyCollisionInfo ) ) )
  66.             {
  67.                 count = true;
  68.             }
  69.             else
  70.             {
  71.                 // OTHERWISE MAKE A DISTANCE CHECK
  72.                 if ( Vector3.Distance( bodyPart.position, newBodyPart.position ) > 0.1f )
  73.                 {
  74.                     count = true;
  75.                 }
  76.  
  77.                 BodyCollision.Remove( bodyPart );
  78.             }
  79.  
  80.             // WHEN THE HIT IS VALID
  81.             if ( count )
  82.             {
  83.                 // CALCULATE THE IMPULSE BETWEEN THIS 2 COLLIDER
  84.                 var impulse = GetImpulse( m );
  85.  
  86.                 // GIVE THE DAMAGE A VALUE, HIPS, SPINE AND HEAD ARE MORE SENSITIVE USUALLY
  87.                 var healthMinus = GetCollisionDamage( impulse, puppetMaster.muscles[ m.muscleIndex ].props.group );
  88.                 // CONVERT THE DAMAGE TO A LOWER USEFUL VALUE
  89.                 var healthDamage = healthMinus * 0.05f;
  90.  
  91.                 // GET THE CURRENT HEALTH
  92.                 var health = currentHealth - healthDamage;
  93.  
  94.                 // SUBTRACT HEALTH
  95.                 ChangeHealth( (int)health );
  96.             }
  97.         }
  98.  
  99.         // JUST MADE THIS FOR TESTING, USUALLY YOU WANT TO CREATE A COMPLEX COUNT SYSTEM ON
  100.         // THE CHARACTER, BASICALLY SENSITIVE PARTS TAKE MORE DAMAGE THAN OTHER
  101.         private float GetCollisionDamage( float rawDamage, Muscle.Group group )
  102.         {
  103.             switch ( group )
  104.             {
  105.                 case Muscle.Group.Hips:
  106.                     return rawDamage * 1.1f;
  107.                 case Muscle.Group.Spine:
  108.                     return rawDamage;
  109.                 case Muscle.Group.Head:
  110.                     return rawDamage * 2f;
  111.                 case Muscle.Group.Arm:
  112.                     return rawDamage * 0.4f;
  113.                 case Muscle.Group.Hand:
  114.                     return rawDamage * 0.2f;
  115.                 case Muscle.Group.Leg:
  116.                     return rawDamage * 0.5f;
  117.                 case Muscle.Group.Foot:
  118.                     return rawDamage * 0.2f;
  119.                 case Muscle.Group.Tail:
  120.                     return rawDamage * 0.2f;
  121.                 case Muscle.Group.Prop:
  122.                     return default;
  123.                 default:
  124.                     return default;
  125.             }
  126.         }
  127.  
  128.         // GETS THE MUSCLE COLLISION INFO TO PREVENT A STRANGE DAMAGE BEHAVIOR WHEN COLLIDING
  129.         private BodyCollisionInfo GetCollisionInfo( int muscle )
  130.         {
  131.             return BodyCollision.Find( x => x.muscle == muscle );
  132.         }
  133.  
  134.         // GETTING THE RAW DAMAGE BASED ON THE COLLIDER MASS
  135.         private float GetImpulse( MuscleCollision m )
  136.         {
  137.             float i = m.collision.impulse.sqrMagnitude;
  138.  
  139.             i /= puppetMaster.muscles[ m.muscleIndex ].rigidbody.mass;
  140.             i *= 0.3f; // Coeficient for evening out for pre-0.3 versions
  141.  
  142.             return i;
  143.         }
  144.     }
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top