Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion;
  4.  
  5. namespace RootMotion.Dynamics {
  6.  
  7.     /// <summary>
  8.     /// Automated prop picking up/dropping for the PuppetMaster.
  9.     /// </summary>
  10.     public abstract class Prop : MonoBehaviour {
  11.  
  12.         #region User Interface
  13.  
  14.         [Tooltip("This has no other purpose but helping you distinguish props by PropRoot.currentProp.propType.")]
  15.         /// <summary>
  16.         /// This has no other purpose but helping you distinguish PropRoot.currentProp by type.
  17.         /// </summary>
  18.         public int propType;
  19.  
  20.         [LargeHeader("Muscle")]
  21.  
  22.         [Tooltip("The Muscle of this prop.")]
  23.         /// <summary>
  24.         /// The Muscle of this prop.
  25.         /// </summary>
  26.         public ConfigurableJoint muscleone;
  27.         public ConfigurableJoint muscletwo;
  28.  
  29.         [Tooltip("The muscle properties that will be applied to the Muscle on pickup.")]
  30.         /// <summary>
  31.         /// The muscle properties that will be applied to the Muscle.
  32.         /// </summary>
  33.         public Muscle.Props muscleProps = new Muscle.Props();
  34.  
  35.         [LargeHeader("Additional Pin")]
  36.  
  37.         [Tooltip("Optinal additional pin, useful for long melee weapons that would otherwise require a lot of muscle force and solver iterations to be swinged quickly. Should normally be without any colliders attached. The position of the pin, it's mass and the pin weight will effect how the prop will handle.")]
  38.         /// <summary>
  39.         /// Optinal additional pin, useful for long melee weapons that would otherwise require a lot of muscle force and solver iterations to be swinged quickly. Should normally be without any colliders attached. The position of the pin, it's mass and the pin weight will effect how the prop will handle.
  40.         /// </summary>
  41.         public ConfigurableJoint additionalPin;
  42.        
  43.         [Tooltip("Target Transform for the additional pin.")]
  44.         /// <summary>
  45.         /// Target Transform for the additional pin.
  46.         /// </summary>
  47.         public Transform additionalPinTarget;
  48.        
  49.         [Tooltip("The pin weight of the additional pin. Increasing this weight will make the prop follow animation better, but will increase jitter when colliding with objects.")]
  50.         /// <summary>
  51.         /// The pin weight of the additional pin. Increasing this weight will make the prop follow animation better, but will increase jitter when colliding with objects.
  52.         /// </summary>
  53.         [Range(0f, 1f)] public float additionalPinWeight = 1f;
  54.  
  55.         /// <summary>
  56.         /// Is this prop picked up and connected to a PropRoot?
  57.         /// </summary>
  58.         public bool isPickedUp { get { return propRoot != null; }}
  59.  
  60.         /// <summary>
  61.         /// Returns the PropRoot that this prop is connected to if it is picked up. If this returns null, the prop is not picked up.
  62.         /// </summary>
  63.         public PropRoot propRoot { get; private set; }
  64.  
  65.         #endregion User Interface
  66.  
  67.         // Picking up/dropping props is done by simply changing PropRoot.currentProp
  68.         public void PickUp(PropRoot propRoot) {
  69.             muscleone.xMotion = xMotion;
  70.             muscleone.yMotion = yMotion;
  71.             muscleone.zMotion = zMotion;
  72.             muscleone.angularXMotion = angularXMotion;
  73.             muscleone.angularYMotion = angularYMotion;
  74.             muscleone.angularZMotion = angularZMotion;
  75.  
  76.             muscletwo.xMotion = xMotion;
  77.             muscletwo.yMotion = yMotion;
  78.             muscletwo.zMotion = zMotion;
  79.             muscletwo.angularXMotion = angularXMotion;
  80.             muscletwo.angularYMotion = angularYMotion;
  81.             muscletwo.angularZMotion = angularZMotion;
  82.            
  83.             this.propRoot = propRoot;
  84.  
  85.             muscleone.gameObject.layer = propRoot.puppetMaster.gameObject.layer;
  86.             //muscletwo.gameObject.layer = propRoot.puppetMaster.gameObject.layer;
  87.             foreach (Collider c in colliders) {
  88.                 if (!c.isTrigger) c.gameObject.layer = muscleone.gameObject.layer;
  89.             }
  90.  
  91.             OnPickUp(propRoot);
  92.         }
  93.  
  94.         // Picking up/dropping props is done by simply changing PropRoot.currentProp
  95.         public void Drop() {
  96.             this.propRoot = null;
  97.  
  98.             OnDrop();
  99.         }
  100.  
  101.         public void StartPickedUp(PropRoot propRoot) {
  102.             this.propRoot = propRoot;
  103.         }
  104.  
  105.         protected virtual void OnPickUp(PropRoot propRoot) {}
  106.         protected virtual void OnDrop() {}
  107.         protected virtual void OnStart() {}
  108.    
  109.         private ConfigurableJointMotion xMotion, yMotion, zMotion, angularXMotion, angularYMotion, angularZMotion;
  110.         private Collider[] colliders = new Collider[0];
  111.  
  112.         protected virtual void Awake() {
  113.             if (transform.position != muscleone.transform.position) {
  114.                 Debug.LogError("Prop target position must match exactly with it's muscle's position!", transform);
  115.             }
  116.  
  117.             xMotion = muscleone.xMotion;
  118.             yMotion = muscleone.yMotion;
  119.             zMotion = muscleone.zMotion;
  120.             angularXMotion = muscleone.angularXMotion;
  121.             angularYMotion = muscleone.angularYMotion;
  122.             angularZMotion = muscleone.angularZMotion;
  123.  
  124.             xMotion = muscletwo.xMotion;
  125.             yMotion = muscletwo.yMotion;
  126.             zMotion = muscletwo.zMotion;
  127.             angularXMotion = muscletwo.angularXMotion;
  128.             angularYMotion = muscletwo.angularYMotion;
  129.             angularZMotion = muscletwo.angularZMotion;
  130.  
  131.             colliders = muscleone.GetComponentsInChildren<Collider>();
  132.         }
  133.  
  134.         void Start() {
  135.  
  136.             if (!isPickedUp) ReleaseJoint();
  137.  
  138.             OnStart();
  139.         }
  140.  
  141.         private void ReleaseJoint() {
  142.             muscleone.connectedBody = null;
  143.             muscleone.targetRotation = Quaternion.identity;
  144.  
  145.             muscletwo.connectedBody = null;
  146.             muscletwo.targetRotation = Quaternion.identity;
  147.  
  148.  
  149.             JointDrive j = new JointDrive();
  150.             j.positionSpring = 0f;
  151.  
  152.             JointDrive k = new JointDrive();
  153.             k.positionSpring = 0f;
  154.             #if UNITY_5_2
  155.             j.mode = JointDriveMode.None;
  156.             #endif
  157.             muscleone.slerpDrive = j;
  158.             muscletwo.slerpDrive = k;
  159.            
  160.             muscleone.xMotion = ConfigurableJointMotion.Free;
  161.             muscleone.yMotion = ConfigurableJointMotion.Free;
  162.             muscleone.zMotion = ConfigurableJointMotion.Free;
  163.             muscleone.angularXMotion = ConfigurableJointMotion.Free;
  164.             muscleone.angularYMotion = ConfigurableJointMotion.Free;
  165.             muscleone.angularZMotion = ConfigurableJointMotion.Free;
  166.  
  167.             muscletwo.xMotion = ConfigurableJointMotion.Free;
  168.             muscletwo.yMotion = ConfigurableJointMotion.Free;
  169.             muscletwo.zMotion = ConfigurableJointMotion.Free;
  170.             muscletwo.angularXMotion = ConfigurableJointMotion.Free;
  171.             muscletwo.angularYMotion = ConfigurableJointMotion.Free;
  172.             muscletwo.angularZMotion = ConfigurableJointMotion.Free;
  173.         }
  174.  
  175.         // Just making sure this GameObject's position and rotation matches with the muscle's in the Editor.
  176.         void OnDrawGizmos() {
  177.             if (muscleone == null) return;
  178.             if (Application.isPlaying) return;
  179.  
  180.             transform.position = muscleone.transform.position;
  181.             transform.rotation = muscleone.transform.rotation;
  182.  
  183.             if (additionalPinTarget != null && additionalPin != null) {
  184.                 additionalPinTarget.position = additionalPin.transform.position;
  185.             }
  186.  
  187.             muscleProps.group = Muscle.Group.Prop;
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement