Next, let's rotate the GameObject touched around the palm of your right hand. This tutorial is a continuation of the "Hold Me Tight" tutorial. If you have not yet comple "Hold Me Tight", please proceed with the following tutorial first.
Sample Repository
- mr-tutorials-for-nreal-light/Rotator at main · karad/mr-tutorials-for-nreal-light https://github.com/karad/mr-tutorials-for-nreal-light/tree/main/Rotator
Run the sample
- Clone Sample Repository, Change current directory to
Rotator
. And Open with Unity. - (If you don't have NRSDK) Download NRSDK 1.8.0 from Download | Nreal
- Open
Build Setting
, change Platform toAndroid
- Open
Project
, selectAssets
>import package
>Custom Package
and importNRSDKForUnityAndroid_1.8.0.unitypackage
. - Check
Build Settings
>Player Settings
by referring to Configure Build Settings - Press
Build
formBuild Settings
panel - Install *.apk on Android or DevKit.
Tutorial
1. Place the TargetCube a little closer
- Select
TargetCube1
fromHierarchy
>Target
- Change
Pos z
to 0.6
2. Change C# script TargetCube.cs
Modify the C# script to reflect the right hand rotation on the TargetCube. Change the TargetCube.cs
code as follows.
using NRKernal;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// Target Cube Script
/// </summary>
public class TargetCube : MonoBehaviour
{
private MeshRenderer meshRender;
void Awake()
{
// Get mesh renderer
meshRender = transform.GetComponent<MeshRenderer>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// Trigger Enter Event Handler
/// </summary>
/// <param name="other"></param>
private void OnTriggerEnter(Collider other)
{
Debug.Log("Enter!");
meshRender.material.color = new Color(0f, 1f, 0f);
}
/// <summary>
/// Trigger Stay Event Handler
/// </summary>
/// <param name="other"></param>
private void OnTriggerStay(Collider other)
{
// Get right hand rotation value
HandState handState = NRInput.Hands.GetHandState(HandEnum.RightHand);
Quaternion handStateThumbTipRotation = handState.GetJointPose(HandJointID.Palm).rotation;
Quaternion targetRotation = transform.rotation;
targetRotation.z = handStateThumbTipRotation.z;
transform.rotation = targetRotation;
}
/// <summary>
/// Trigger Exit Event Handler
/// </summary>
/// <param name="other"></param>
private void OnTriggerExit(Collider other)
{
Debug.Log("Exit!");
meshRender.material.color = new Color(1f, 1f, 1f);
}
}
3. Change C# script HandController.cs
Next, let's also modify the right hand controller. Change the touch point from index finger to palm. Change the HandController.cs
code as follows.
using NRKernal;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Hand Controller for Tip
/// </summary>
public class HandController : MonoBehaviour
{
/// <summary>
/// IndexTip GameObject
/// </summary>
public GameObject hand_R_IndexTip;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Get Right Hand
HandState handState = NRInput.Hands.GetHandState(HandEnum.RightHand);
// Get Index Tip Position of right hand
Vector3 handStatePalmPosition = handState.GetJointPose(HandJointID.Palm).position;
// Set Hand R IndexTip Sphere position
hand_R_IndexTip.transform.position = handStatePalmPosition;
}
}
4. Create a display area for rotation value
Let's also create a display area to get a numerical value for the right hand rotation value. Create C# Script
in the asset with the file name "DisplayInfo.cs". Write the code as follows.
using NRKernal;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Display about user actions
/// </summary>
public class DisplayInfo : MonoBehaviour
{
/// <summary>
///
/// </summary>
public Text info;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Numeric display of right hand palm rotation value
HandState handState = NRInput.Hands.GetHandState(HandEnum.RightHand);
if (handState != null)
{
info.text = handState.GetJointPose(HandJointID.Palm).rotation.z.ToString();
};
}
}
5. Put Text on the scene
Put Text object for displaying the cube rotation value.
- Put
Canvas
fromCreate
>UI
- Put
Text
as a child ofCanvas
- Change Text to “Info” on
Inspector
panel. - Attach
DisplayInfo.cs
onInspector
panel ofCanvas
. - Set
info
toInfo
Text Object.
6. Run the tutorial
- Press
Play
button and run the tutorial.
7. Build
- Press
Build
formBuild Settings
panel - Install *.apk on Android or DevKit.
Top comments (0)