VRTK功能教学(二):Unity3DVRTK手柄瞬移和UI交互射线切换功能丨3D模型射线交互切换丨直线和曲线的切换
`
文章目录
介绍
本文:针对VRTK的手柄交互功能,瞬移和UI交互的时候两个射线无法有效切换的问题
代码展示
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using VRTK;
public class VRTK_SwitchPointLine : MonoBehaviour
{
public VRTK_BezierPointerRenderer bezierRender;
public VRTK_StraightPointerRenderer straightRender;
public VRTK_Pointer mainPointer;
public string InteractHoverTag = "ModeObj";
private VRTK_ControllerEvents mEvent;
private VRTK_ShotLine cacheShowLabel;
public UnityAction<string> r_RayAction = null;
// public UnityEvent TipAccordingObjevent;
private void Awake()
{
mEvent = GetComponent<VRTK_ControllerEvents>();
mainPointer.activateOnEnable = false;
bezierRender.enabled = false;
straightRender.enabled = false;
}
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out var hit))
{
VRTK_ShotLine temp = hit.transform.GetComponent<VRTK_ShotLine>();
//直线射线物体使用方式为轻触触摸板 对准带着规定标签的物体 射线转换为直线
if (temp != null && mEvent.touchpadTouched)
{
if (hit.transform.tag.Equals(this.InteractHoverTag) || hit.transform.tag.Equals("PositionControl"))
{
temp.LineEnter(this.mEvent);
if (mEvent.triggerClicked)
{
Debug.Log(hit.collider.name);
r_RayAction?.Invoke(hit.collider.name);
}
}
if (cacheShowLabel == null)
{
temp.LineEnter();
}
else
if (cacheShowLabel != null && cacheShowLabel != temp)
{
cacheShowLabel.LineExit();
temp.LineEnter();
}
cacheShowLabel = temp;
mainPointer.pointerRenderer = straightRender;
}
else
{
//否则射线为曲线
if (cacheShowLabel != null)
{
cacheShowLabel.LineExit();
cacheShowLabel = null;
}
//判断检测到Ui的交互时射线转换为直线
if (hit.transform.GetComponent<VRTK_UICanvas>())
{
mainPointer.pointerRenderer = straightRender;
}
else if (hit.transform.tag == "canPoint")
{
mainPointer.pointerRenderer = straightRender;
}
else
{
mainPointer.pointerRenderer = bezierRender;
}
}
}
else
{
if (cacheShowLabel != null)
{
cacheShowLabel.LineExit();
cacheShowLabel = null;
}
mainPointer.pointerRenderer = bezierRender;
}
if (mEvent != null && cacheShowLabel != null)
{
if (mEvent.triggerClicked)
{
cacheShowLabel.Execute();
cacheShowLabel = null;
}
}
if (mainPointer.pointerRenderer == straightRender)
{
bezierRender.enabled = false;
straightRender.enabled = true;
mainPointer.activateOnEnable = true;
mainPointer.holdButtonToActivate = false;
mainPointer.activationButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
mainPointer.selectionButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
straightRender.cursorVisibility = VRTK_BasePointerRenderer.VisibilityStates.AlwaysOn;
straightRender.tracerVisibility = VRTK_BasePointerRenderer.VisibilityStates.AlwaysOn;
}
else
{
bezierRender.enabled = true;
straightRender.enabled = false;
mainPointer.activateOnEnable = false;
mainPointer.holdButtonToActivate = true;
mainPointer.activationButton = VRTK_ControllerEvents.ButtonAlias.TouchpadPress;
mainPointer.selectionButton = VRTK_ControllerEvents.ButtonAlias.TouchpadPress;
straightRender.cursorVisibility = VRTK_BasePointerRenderer.VisibilityStates.OnWhenActive;
straightRender.tracerVisibility = VRTK_BasePointerRenderer.VisibilityStates.OnWhenActive;
}
}
}
还有这一个脚本,这个脚本本来是为了模型交互使用的,但是如果不加此脚本会报错很多,一个个删除挺麻烦的,如果不需要模型交互把此脚本创建出来不用管就好了
using VRTK;
public interface VRTK_ShotLine
{
void LineEnter();
void LineExit();
void Execute();
void LineEnter(VRTK_ControllerEvents events);
}
配置方法
基础配置
首先我们手柄的控制器上挂载的组件不能少
其中的Switch Point Line为我们编译的脚本
三个直接挂载手柄控制器就可以
UI交互配置
UI交互的话除了要配置基础配置还要在想要交互的Canvas上挂载脚本UICanvas
3D物体模型交互
模型交互除了要配置基础配置以外还需要把我们的VRTK_ShotLine脚本挂载在需要交互的物体身上,还有标签,就是我们脚本中定义的名称ModeObj。新手注意:一定要添加碰撞体。
总结
例如:以上就是今天要讲的内容,本文介绍了VRTk的手柄射线功能使用。