87 lines
2.0 KiB
C#
87 lines
2.0 KiB
C#
using System;
|
|
using HurricaneVR.Framework.Core;
|
|
using HurricaneVR.Framework.Core.Grabbers;
|
|
using HurricaneVR.Framework.Weapons.Guns;
|
|
using UnityEngine;
|
|
|
|
public class GrenadeActivator : MonoBehaviour
|
|
{
|
|
public event Action OnReady = delegate {};
|
|
|
|
[SerializeField] private HVRGrabbable grabbable;
|
|
[SerializeField] private HVRGrabbable masterGrabbable;
|
|
[SerializeField] private HingeJoint joint;
|
|
[SerializeField] private Rigidbody rig;
|
|
[SerializeField] private HVRCockingHandle cockingHandle;
|
|
[SerializeField] private Vector3 anchor;
|
|
|
|
private bool isActivated;
|
|
private Vector3 offsetAnchor;
|
|
private float minJointLimit;
|
|
private float maxJointLimit;
|
|
|
|
private void Start()
|
|
{
|
|
grabbable.Grabbed.AddListener(Grab);
|
|
grabbable.Released.AddListener(Release);
|
|
|
|
cockingHandle.EjectReached.AddListener(Activate);
|
|
|
|
var limits = joint.limits;
|
|
minJointLimit = limits.min;
|
|
maxJointLimit = limits.max;
|
|
|
|
anchor = joint.connectedAnchor;
|
|
joint.autoConfigureConnectedAnchor = false;
|
|
joint.connectedAnchor = anchor;
|
|
|
|
offsetAnchor = anchor - transform.localPosition;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
grabbable.Grabbed.RemoveListener(Grab);
|
|
grabbable.Released.RemoveListener(Release);
|
|
|
|
cockingHandle.EjectReached.RemoveListener(Activate);
|
|
}
|
|
|
|
private void Grab(HVRGrabberBase arg0, HVRGrabbable arg1)
|
|
{
|
|
var limit = joint.limits;
|
|
limit.min = 0;
|
|
limit.max = 0.01f;
|
|
joint.limits = limit;
|
|
rig.useGravity = false;
|
|
}
|
|
|
|
private void Release(HVRGrabberBase arg0, HVRGrabbable arg1)
|
|
{
|
|
var limit = joint.limits;
|
|
limit.min = minJointLimit;
|
|
limit.max = maxJointLimit;
|
|
joint.limits = limit;
|
|
rig.useGravity = true;
|
|
}
|
|
|
|
public void Activate()
|
|
{
|
|
if (isActivated) return;
|
|
isActivated = true;
|
|
masterGrabbable.LinkedGrabbables.Remove(grabbable);
|
|
grabbable.ForceRelease();
|
|
Destroy(gameObject);
|
|
OnReady?.Invoke();
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
var a = joint.connectedAnchor;
|
|
a.x = transform.localPosition.x + offsetAnchor.x;
|
|
a.y = anchor.y;
|
|
a.z = anchor.z;
|
|
joint.connectedAnchor = a;
|
|
}
|
|
}
|