Unity Advanced Guide - bhaptics/haptic-library GitHub Wiki
-
Video Tutorial: https://www.youtube.com/watch?v=C7ysf4mKWqQ
-
By changing the option in the inspector, a dynamic haptic pattern is generated.
- 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;
-
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.
-
In the scene, it uses two scripts
HapticSender.cs
andHapticReceiver.cs
- This script is to define haptic patterns, and it can be used in the Player's scripts, Weapons scripts, or bullet's scripts.
- This script is to define actual target location such as head, body or arms, a collider should be located in the sample script.
- 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);
}
}
- BhapticsCharacterController.cs
var bullet = (GameObject)Instantiate(bulletPrefab, shootingPoint.transform.position, shootingPoint.transform.rotation);
bullet.GetComponent<Rigidbody>().velocity = shootingPoint.forward * 10f;
Destroy(bullet, 5f);