void teleport()
    {
        Vector2 originalPos = this.transform.position;
        Vector2 newPos      = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (newPos.x > ar_manager.x_max || newPos.x < ar_manager.x_min ||
            newPos.y > ar_manager.y_max || newPos.y < ar_manager.y_min)
        {
            return;
        }

        this.transform.position = newPos;
        GameObject player_surrogate = Instantiate(teleportPlayerSurrogatePrefab,
                                                  originalPos,
                                                  Quaternion.identity);

        player_surrogate.GetComponent <SpriteRenderer>().sprite = this.GetComponent <SpriteRenderer>().sprite;
        player_surrogate.transform.localScale = this.transform.localScale;
        player_surrogate.GetComponent <Animator>().SetTrigger("despawn");
        this.GetComponent <Animator>().SetTrigger("spawn");

        TeleportLine line = Instantiate(teleportLinePrefab).GetComponent <TeleportLine>();

        line.setPoints(originalPos, newPos);
        this.GetComponent <Rigidbody2D>().velocity = Vector3.zero;        //now not falling

        if (teleportEvent != null)
        {
            teleportEvent();
        }
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        ControllerDevice = SteamVR_Controller.Input((int)TrackObj.index);

        //(only Lefthand)--->for Teleporting
        if (forHand == Hand.LeftHand)
        {
            //To show the moving place
            if (ControllerDevice.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
            {
                //Debug.Log("ControllerDevice.rigibody.velocity:" + gameObject.GetComponent<Rigidbody>().velocity + "\nControllerDevice.rigibody.angularVelocity:" + gameObject.GetComponent<Rigidbody>().angularVelocity);
                isCanTeleport = false;
                TeleportLineRender.gameObject.SetActive(true);
                RaycastHit hitInfo;
                if (Physics.Raycast(transform.position, transform.forward, out hitInfo, 15.0f, TeleporterLayer))
                {
                    TeleporterLocation = hitInfo.point;
                    TeleportLineRender.numPositions = 2;
                    TeleportLineRender.SetPosition(0, transform.position);
                    TeleportLineRender.SetPosition(1, TeleporterLocation);
                    isCanTeleport = true;
                }
                else
                {
                    Vector3    TempDirectionPoint = transform.position + transform.forward * 15;
                    RaycastHit groundRayhit;
                    if (Physics.Raycast(TempDirectionPoint, -Vector3.up, out groundRayhit, 17.0f, TeleporterLayer))
                    {
                        //TeleporterLocation = new Vector3(transform.position.x + transform.forward.x * 15, groundRayhit.transform.position.y, transform.position.z + transform.forward.z * 15);
                        TeleporterLocation = groundRayhit.point;
                        TeleportLine.DrawBezierLine(TeleportLineRender, transform.position, TempDirectionPoint, TeleporterLocation, 20);
                        isCanTeleport = true;
                    }
                    else
                    {
                        TeleportLineRender.numPositions = 2;
                        TeleportLineRender.SetPosition(0, transform.position);
                        TeleportLineRender.SetPosition(1, TempDirectionPoint);
                        //TeleporterLocation = Player.transform.position;
                        isCanTeleport = false;
                    }
                }
                if (isCanTeleport)
                {
                    TeleporterTargetObject.transform.position = TeleporterLocation + (Vector3.up * 0.001f);
                    TeleporterTargetObject.SetActive(true);
                }
            }

            //To teleport the player
            if (ControllerDevice.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
            {
                if (isCanTeleport)
                {
                    Player.transform.position = TeleporterLocation;
                    TeleportLineRender.gameObject.SetActive(false);
                    TeleporterTargetObject.SetActive(false);
                }
            }

            //Romove teleport
            if (ControllerDevice.GetTouchUp(SteamVR_Controller.ButtonMask.Touchpad))
            {
                TeleportLineRender.gameObject.SetActive(false);
                TeleporterTargetObject.SetActive(false);
            }
        }

        //(only RightHand) --->for menu
        if (forHand == Hand.RightHand)
        {
            //memu apearing
            if (ControllerDevice.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
            {
                MenuAppear();
            }
            //Menu disappear
            if (ControllerDevice.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
            {
                MenuDisappear();
            }
            //SpawnObject
            if (ControllerDevice.GetPress(SteamVR_Controller.ButtonMask.Trigger))
            {
                if (ControllerDevice.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
                {
                    //Record the Axis of x and y on firstTouch
                    TouchLast_x = ControllerDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x;
                    TouchLast_y = ControllerDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).y;
                }
                if (ControllerDevice.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
                {
                    //Record the Axis x on its moving distance
                    Touchcurrent_x = ControllerDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).x;
                    distance_x     = Touchcurrent_x - TouchLast_x;
                    TouchLast_x    = Touchcurrent_x;
                    SwipeSum_x    += distance_x;

                    //Record the Axis y on its moving distance
                    Touchcurrent_y = ControllerDevice.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad).y;
                    distance_y     = Touchcurrent_y - TouchLast_y;
                    TouchLast_y    = Touchcurrent_y;
                    SwipeSum_y    += distance_y;

                    //if Axis x's swipe distance >0.5, then change selected Object
                    if (!hasSwipeRight_x)
                    {
                        if (SwipeSum_x > 0.5f)
                        {
                            SwipeSum_x = 0;
                            SwipeRight();
                            hasSwipeRight_x = true;
                            hasSwipeLeft_x  = false;
                        }
                    }
                    if (!hasSwipeLeft_x)
                    {
                        if (SwipeSum_x < -0.5f)
                        {
                            SwipeSum_x = 0;
                            SwipeLeft();
                            hasSwipeRight_x = false;
                            hasSwipeLeft_x  = true;
                        }
                    }

                    //if Axis y's swipe distance >0.5, then change selected Munu
                    if (!hasSwipeRight_y)
                    {
                        if (SwipeSum_y > 0.5f)
                        {
                            SwipeSum_y = 0;
                            SwipeUP();
                            hasSwipeRight_y = true;
                            hasSwipeLeft_y  = false;
                        }
                    }
                    if (!hasSwipeLeft_y)
                    {
                        if (SwipeSum_y < -0.5f)
                        {
                            SwipeSum_y = 0;
                            SwipeDown();
                            hasSwipeRight_y = false;
                            hasSwipeLeft_y  = true;
                        }
                    }
                }
                //if leave the touchpad ,reSet the touch number
                if (ControllerDevice.GetTouchUp(SteamVR_Controller.ButtonMask.Touchpad))
                {
                    Touchcurrent_x  = 0;
                    TouchLast_x     = 0;
                    distance_x      = 0;
                    SwipeSum_x      = 0;
                    hasSwipeLeft_x  = false;
                    hasSwipeRight_x = false;
                }
                if (ControllerDevice.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
                {
                    SpawnObject();
                }
            }
        }
    }