6

Unity OpenVR 虚拟现实入门四:通过脚本控制手与控制器

 3 years ago
source link: https://blog.walterlv.com/post/unity-openvr-starting-4.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Unity OpenVR 虚拟现实入门四:通过脚本控制手与控制器

发布于 2021-05-16 09:49
更新于 2021-05-16 09:50

在 Unity 的帮助下,虚拟现实应用的开发非常容易。不过国内竟然还是没有什么教程,所以这里就来一点入门的,适合新手。

本文将基于第三篇的简单场景,打开和关闭控制器的显示。


系列博客:

本文继续第三篇的内容。

在“Player”上添加脚本。我们在“Update”中简单添加一些代码:

using UnityEngine;

using Valve.VR;
using Valve.VR.InteractionSystem;

public class PlayerDemoScript : MonoBehaviour
{
    public bool showControllers;

    void Update()
    {
        foreach (var hand in Player.instance.hands)
        {
            if (showControllers)
            {
                hand.ShowController();
                hand.SetSkeletonRangeOfMotion(EVRSkeletalMotionRange.WithController);
            }
            else
            {
                hand.HideController();
                hand.SetSkeletonRangeOfMotion(EVRSkeletalMotionRange.WithoutController);
            }
        }
    }
}

这样,当 showControllers 被设置为 true 时,可以同时显示手与控制器,当设置为 false 时,则只显示手。

这里 ShowController 是显示控制器,HideController 是隐藏控制器。后面的 SetSkeletonRangeOfMotion 是让手的骨骼动画适配控制器,如果指定为 WithController 则会在运动手指的时候握紧时只会握住控制器,而不会穿模到控制器里面;反之,握紧的时候则不考虑控制器的位置,会穿模。

手握住控制器
▲ 手握住控制器

运行场景,当我们在“检查器”中勾选“showControllers”时,会在场景中看到手握住控制器。

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/unity-openvr-starting-4.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK