Unity Advanced Guide - bhaptics/haptic-library GitHub Wiki

Generating dynamic haptic patterns from the haptic pattern files. (TactSuit)

image

img

  • In the script, you can do as below.
using Bhaptics.Tact.Unity;
using UnityEngine;

public class RotationalExample : MonoBehaviour
{
    [Range(0.2f, 5f)]
    public float intensity = 1f;
    [Range(0.2f, 5f)]
    public float duration = 1f;

    [Range(0, 360f)]
    public float angleX;
    [Range(-0.5f, 0.5f)]
    public float offsetY;

    public VestHapticClip clip;


    public void Play()
    {
        if (clip != null)
        {
            clip.Play(intensity, duration, angleX, offsetY);
        }
    }
}
  • Simple mathematics to calculate the angleX and offsetY can be done as below.
  var angle = BhapticsUtils.Angle(targetDir, targetForward);
  var offsetY = (contactPos.y - targetPos.y) / targetHeight;

Demo Shooter Example

  • The scene files are located at Assets/Bhaptics/SDK/Examples/Scenes/2. Demo Shooter.unity

  • Demo Shooter is an FPS sample showing how to use bHaptics SDK.   img

  • In the scene, it uses two scripts HapticSender.cs and HapticReceiver.cs

HapticSender

  • This script is to define haptic patterns, and it can be used in the Player's scripts, Weapons scripts, or bullet's scripts.  image

HapticReceiver

  • This script is to define actual target location such as head, body or arms, a collider should be located in the sample script. 

image

For raycast shooting

  • BhapticsCharacterController.cs
  • For raycast shooting, it checks HapticReceiver while shooting.
if (Physics.Raycast(ray, out raycastHit, length))
{
    var detect = raycastHit.collider.gameObject.GetComponent<HapticReceiver>();
    var pos = PositionTag.Default;

    if (detect == null)
    {
        ///// THIS IS ONLY FOR DEMO CASE.
        var custom = raycastHit.collider.gameObject.GetComponent<BhapticsCustomTactReceiver>();
        if (custom != null)
        {
            custom.ReflectHandle(raycastHit.point, tactSender);
            return;
        }
    }
    else
    {
        pos = detect.PositionTag;
    }


    if (tactSender != null)
    {
        tactSender.Play(pos, raycastHit);
    }
}

Bullet based shooting

  • BhapticsCharacterController.cs
var bullet = (GameObject)Instantiate(bulletPrefab, shootingPoint.transform.position, shootingPoint.transform.rotation);
bullet.GetComponent<Rigidbody>().velocity = shootingPoint.forward * 10f;

Destroy(bullet, 5f);
⚠️ **GitHub.com Fallback** ⚠️