Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

50 lines
1.7 KiB

namespace VRTK.Examples
{
using UnityEngine;
public class Sword : VRTK_InteractableObject
{
private float impactMagnifier = 120f;
private float collisionForce = 0f;
private float maxCollisionForce = 4000f;
private VRTK_ControllerReference controllerReference;
public float CollisionForce()
{
return collisionForce;
}
public override void Grabbed(VRTK_InteractGrab grabbingObject)
{
base.Grabbed(grabbingObject);
controllerReference = VRTK_ControllerReference.GetControllerReference(grabbingObject.controllerEvents.gameObject);
}
public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject)
{
base.Ungrabbed(previousGrabbingObject);
controllerReference = null;
}
protected override void OnEnable()
{
base.OnEnable();
controllerReference = null;
interactableRigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
private void OnCollisionEnter(Collision collision)
{
if (VRTK_ControllerReference.IsValid(controllerReference) && IsGrabbed())
{
collisionForce = VRTK_DeviceFinder.GetControllerVelocity(controllerReference).magnitude * impactMagnifier;
var hapticStrength = collisionForce / maxCollisionForce;
VRTK_ControllerHaptics.TriggerHapticPulse(controllerReference, hapticStrength, 0.5f, 0.01f);
}
else
{
collisionForce = collision.relativeVelocity.magnitude * impactMagnifier;
}
}
}
}