Unity学习经验:可直接使用UnityVR射线传送

60 篇文章 2 订阅
2 篇文章 0 订阅
脚本放在右手上Controller (right)上

#define Pingdi
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Valve.VR;

public class Teleport : MonoBehaviour
{
    private bool move;

#if Pingdi
    private float gaodu;
#endif

    private LineRenderer line, line2;

    private GameObject Cube;

    private GameObject box, camera;
    private Ray ray1, ray2;

    private SteamVR_Input_Sources handType;
    private SteamVR_Action_Boolean teleportAction;

    private RaycastHit hit1, hit2;

    private Vector3 v, vv;

    private Vector3[] vvv = new Vector3 [100];

    private float x, z;

    private bool boo;

    private Vector3 MinV = new Vector3(int.MinValue, int.MinValue, int.MinValue);

    // Start is called before the first frame update
    void Start()
    {
#if Pingdi
        gaodu = gameObject.transform.position.y;
#endif

        handType = SteamVR_Input_Sources.RightHand;
        teleportAction = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("default", "Teleport");
        Cube = new GameObject();
        Cube.name = "Cube";
        Cube.transform.SetParent(gameObject.transform);
        Cube.transform.localPosition = new Vector3(0, 0, 10);
        camera = gameObject.transform.parent.Find("Camera").gameObject;
        box = gameObject.transform.parent.gameObject;
        line = new GameObject().AddComponent<LineRenderer>();
        line.positionCount = 2;
        line.startWidth = 0.1f;
        line2 = new GameObject().AddComponent<LineRenderer>();
        line2.positionCount = 100;
        line2.startWidth = 0.1f;
        line.SetPosition(0, MinV);
        line.SetPosition(1, MinV);
        for (int i = 0; i < 100; i++)
        {
            vvv[i] = MinV;
        }
        line2.SetPositions(vvv);
    }

    // Update is called once per frame
    void Update()
    {
        if (teleportAction.GetState(handType))
        {
            ray1 = new Ray(gameObject.transform.position, gameObject.transform.forward);
            ray2 = new Ray(Cube.transform.position, Vector3.down);
            if (Physics.Raycast(ray1, out hit1))
            {
                line.SetPosition(0, gameObject.transform.position);
                line.SetPosition(1, hit1.point);
                line2.gameObject.SetActive(false);
                line.gameObject.SetActive(true);

                if (hit1.transform.gameObject.tag == "dimian")
                {
                    v = hit1.point;
                    line.material.color = Color.green;
                    move = true;
                }
                else
                {
                    line.material.color = Color.red;
                    move = false;
                }
            }
            else if (Physics.Raycast(ray2, out hit2))
            {
                for (float i = 0; i < 100; i++)
                {
                    vv = (gameObject.transform.position * (1 - (i / 100.0f)) * (1 - (i / 100.0f))) +
                         (2.0f * (i / 100.0f) * (1 - (i / 100.0f)) * Cube.transform.position) +
                         ((i / 100) * (i / 100) * hit2.point);
                    vvv[(int) i] = vv;
                }

                line2.SetPositions(vvv);
                line2.gameObject.SetActive(true);
                line.gameObject.SetActive(false);

                if (hit2.transform.gameObject.tag == "dimian")
                {
                    v = hit2.point;
                    line2.material.color = Color.green;
                    move = true;
                }
                else
                {
                    line2.material.color = Color.red;
                    move = false;
                }
            }
        }

        if (teleportAction.GetStateUp(handType))
        {
            line.gameObject.SetActive(false);
            line2.gameObject.SetActive(false);
            x = box.transform.position.x - camera.transform.position.x;
            z = box.transform.position.z - camera.transform.position.z;
            v.x += x;
            v.z += z;
#if Pingdi
            v.y = gaodu;
#endif
            if (move)
            {
                box.transform.position = v;
            }

            line.SetPosition(0, MinV);
            line.SetPosition(1, MinV);
            for (int i = 0; i < 100; i++)
            {
                vvv[i] = MinV;
            }
            line2.SetPositions(vvv);
        }
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值