Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取vive motion cotroller指向的位置(在unity中)_C#_Virtual Reality_Htc Vive - Fatal编程技术网

C# 如何获取vive motion cotroller指向的位置(在unity中)

C# 如何获取vive motion cotroller指向的位置(在unity中),c#,virtual-reality,htc-vive,C#,Virtual Reality,Htc Vive,我想在unity中制作一个VR游戏。我有一些基本的东西,比如门、抽屉、枪和一个对象选择器上面的脚本。我想做一个基础构建器/编辑器,但我完全不知道如何获得控制器指向的位置(如激光指针) 项目位于c#中,您必须从控制器向对象进行光线投射 为了简化您的工作,我建议您使用VRTK工具包,您只需将其作为断言添加到unity项目中即可: 该工具包包含很多功能,如激光点、抓图等 我希望这对你有帮助 编辑: 以下是VRTK工具包中激光指针的一个非常好的示例: using UnityEngine; using

我想在unity中制作一个VR游戏。我有一些基本的东西,比如门、抽屉、枪和一个对象选择器上面的脚本。我想做一个基础构建器/编辑器,但我完全不知道如何获得控制器指向的位置(如激光指针)
项目位于c#

中,您必须从控制器向对象进行光线投射

为了简化您的工作,我建议您使用VRTK工具包,您只需将其作为断言添加到unity项目中即可:

该工具包包含很多功能,如激光点、抓图等

我希望这对你有帮助

编辑:

以下是VRTK工具包中激光指针的一个非常好的示例:

using UnityEngine;
using System.Collections;

public class ViveCursor : MonoBehaviour
{
    public enum AxisType
    {
        XAxis,
        ZAxis
    }

    public Color color;
    public float thickness = 0.002f;
    public AxisType facingAxis = AxisType.XAxis;
    public float length = 100f;
    public bool showCursor = true;

    GameObject holder;
    GameObject pointer;
    GameObject cursor;

    Vector3 cursorScale = new Vector3(0.05f, 0.05f, 0.05f);
    float contactDistance = 0f;
    Transform contactTarget = null;

    void SetPointerTransform(float setLength, float setThicknes)
    {
        //if the additional decimal isn't added then the beam position glitches
        float beamPosition = setLength / (2 + 0.00001f);

        if (facingAxis == AxisType.XAxis)
        {
            pointer.transform.localScale = new Vector3(setLength, setThicknes, setThicknes);
            pointer.transform.localPosition = new Vector3(beamPosition, 0f, 0f);
            if (showCursor)
            {
                cursor.transform.localPosition = new Vector3(setLength - cursor.transform.localScale.x, 0f, 0f);
            }
        }
        else
        {
            pointer.transform.localScale = new Vector3(setThicknes, setThicknes, setLength);
            pointer.transform.localPosition = new Vector3(0f, 0f, beamPosition);

            if (showCursor)
            {
                cursor.transform.localPosition = new Vector3(0f, 0f, setLength - cursor.transform.localScale.z);
            }
        }
    }

    // Use this for initialization
    void Start()
    {
        Material newMaterial = new Material(Shader.Find("Unlit/Color"));
        newMaterial.SetColor("_Color", color);

        holder = new GameObject();
        holder.transform.parent = this.transform;
        holder.transform.localPosition = Vector3.zero;

        pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
        pointer.transform.parent = holder.transform;
        pointer.GetComponent<MeshRenderer>().material = newMaterial;

        pointer.GetComponent<BoxCollider>().isTrigger = true;
        pointer.AddComponent<Rigidbody>().isKinematic = true;
        pointer.layer = 2;

        if (showCursor)
        {
            cursor = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            cursor.transform.parent = holder.transform;
            cursor.GetComponent<MeshRenderer>().material = newMaterial;
            cursor.transform.localScale = cursorScale;

            cursor.GetComponent<SphereCollider>().isTrigger = true;
            cursor.AddComponent<Rigidbody>().isKinematic = true;
            cursor.layer = 2;
        }

        SetPointerTransform(length, thickness);
    }

    float GetBeamLength(bool bHit, RaycastHit hit)
    {
        float actualLength = length;

        //reset if beam not hitting or hitting new target
        if (!bHit || (contactTarget && contactTarget != hit.transform))
        {
            contactDistance = 0f;
            contactTarget = null;
        }

        //check if beam has hit a new target
        if (bHit)
        {
            if (hit.distance <= 0)
            {

            }
            contactDistance = hit.distance;
            contactTarget = hit.transform;
        }

        //adjust beam length if something is blocking it
        if (bHit && contactDistance < length)
        {
            actualLength = contactDistance;
        }

        if (actualLength <= 0)
        {
            actualLength = length;
        }

        return actualLength; ;
    }

    void Update()
    {
        Ray raycast = new Ray(transform.position, transform.forward);

        RaycastHit hitObject;
        bool rayHit = Physics.Raycast(raycast, out hitObject);

        float beamLength = GetBeamLength(rayHit, hitObject);
        SetPointerTransform(beamLength, thickness);
    }
}
使用UnityEngine;
使用系统集合;
公共类活体动物:单行为
{
公共枚举类型
{
XAxis,
扎克西斯
}
公共色彩;
公共浮子厚度=0.002f;
公共AxisType facingAxis=AxisType.XAxis;
公共浮子长度=100f;
public bool showCursor=true;
游戏对象持有者;
游戏对象指针;
游戏对象光标;
矢量3光标刻度=新矢量3(0.05f,0.05f,0.05f);
浮动接触距离=0f;
转换contactTarget=null;
无效设置点转换(浮点设置长度、浮点设置厚度)
{
//如果未添加额外的小数点,则光束位置会出现故障
浮动光束位置=设定长度/(2+0.00001f);
if(facingAxis==AxisType.XAxis)
{
pointer.transform.localScale=新向量3(setLength、setThicknes、setThicknes);
pointer.transform.localPosition=新向量3(beamPosition,0f,0f);
如果(显示光标)
{
cursor.transform.localPosition=newvector3(setLength-cursor.transform.localScale.x,0f,0f);
}
}
其他的
{
pointer.transform.localScale=新向量3(setThicknes、setThicknes、setLength);
pointer.transform.localPosition=新向量3(0f,0f,beamPosition);
如果(显示光标)
{
cursor.transform.localPosition=newvector3(0f,0f,setLength-cursor.transform.localScale.z);
}
}
}
//用于初始化
void Start()
{
材质newMaterial=新材质(Shader.Find(“unlight/Color”);
newMaterial.SetColor(“\u Color”,Color);
holder=新游戏对象();
holder.transform.parent=this.transform;
holder.transform.localPosition=Vector3.0;
指针=GameObject.CreatePrimitive(PrimitiveType.Cube);
pointer.transform.parent=holder.transform;
pointer.GetComponent().material=newMaterial;
指针.GetComponent().isTrigger=true;
pointer.AddComponent().iskinetic=true;
pointer.layer=2;
如果(显示光标)
{
游标=GameObject.CreatePrimitive(PrimitiveType.Sphere);
cursor.transform.parent=holder.transform;
cursor.GetComponent().material=newMaterial;
cursor.transform.localScale=cursorScale;
cursor.GetComponent().isTrigger=true;
cursor.AddComponent().iskinetic=true;
cursor.layer=2;
}
设定值转换(长度、厚度);
}
float GetBeamLength(bool bHit、raycast hit)
{
浮动实际长度=长度;
//如果光束未击中或击中新目标,则重置
如果(!bHit | |(contactTarget&&contactTarget!=命中.变换))
{
接触距离=0f;
contactTarget=null;
}
//检查光束是否击中新目标
如果(bHit)
{
如果(命中距离)