using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BNG {
///
/// Controls collision state of Hand Model.
///
public class HandCollision : MonoBehaviour {
///
/// Used to determine if pointing or gripping
///
public HandController HandControl;
///
/// Used to determine if Grabbing something
///
public Grabber HandGrabber;
///
/// If false we will not check for Hand Collision at all
///
public bool EnableHandCollision = true;
///
/// Should we enable hand colliders when pointing
///
public bool EnableCollisionOnPoint = true;
///
/// Should we enable hand colliders when making a fist
///
public bool EnableCollisionOnFist = true;
///
/// Should we enable hand colliders at all times (still respects EnableCollisionDuringGrab)
///
public bool EnableCollisionOnAllPoses = false;
///
/// Set to false to Disable Hand Colliders during grab or remote grab
///
public bool EnableCollisionDuringGrab = false;
public float PointAmount;
public float GripAmount;
public bool MakingFist;
// Colliders to keep track of
List handColliders;
void Start() {
handColliders = new List();
var tempColliders = GetComponentsInChildren(true);
// Only accept non-trigger colliders.
foreach(var c in tempColliders) {
if(!c.isTrigger) {
handColliders.Add(c);
}
}
}
void Update() {
if(!EnableHandCollision) {
return;
}
bool grabbing = HandGrabber != null && HandGrabber.HoldingItem;
bool makingFist = HandControl != null && HandControl.GripAmount > 0.9f && (HandControl.PointAmount < 0.1 || HandControl.PointAmount > 1);
MakingFist = makingFist;
PointAmount = HandControl != null ? HandControl.PointAmount : 0;
GripAmount = HandControl != null ? HandControl.GripAmount : 0;
bool pointing = HandControl != null && HandControl.PointAmount > 0.9f && HandControl.GripAmount > 0.9f;
for (int x = 0; x < handColliders.Count; x++) {
Collider col = handColliders[x];
// Immediately disable collider if no collision on grab
if (EnableCollisionDuringGrab == false && grabbing) {
col.enabled = false;
continue;
}
// Immediately disable collider if we just released an item.
// This is so we don't enable the collider right when we are trying to drop something
if(HandGrabber != null && (Time.time - HandGrabber.LastDropTime < 0.5f )) {
col.enabled = false;
continue;
}
bool enableCollider = false;
if (EnableCollisionDuringGrab && grabbing) {
enableCollider = true;
}
else if (EnableCollisionOnPoint && pointing) {
enableCollider = true;
}
else if (EnableCollisionOnFist && makingFist) {
enableCollider = true;
}
else if (EnableCollisionOnAllPoses) {
enableCollider = true;
}
col.enabled = enableCollider;
}
}
}
}