Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Valve.VR;
- public class Behaviour : MonoBehaviour {
- public SteamVR_Input_Sources handType;
- public SteamVR_Action_Boolean teleportAction;
- public SteamVR_Behaviour_Pose controllerPose;
- public SteamVR_Action_Boolean grabAction;
- public LineRenderer RayPrefab;
- private LineRenderer Ray;
- private Vector3 hitPoint;
- public Transform cameraRigTransform;
- public GameObject teleportReticlePrefab;
- private GameObject reticle;
- private Transform teleportReticleTransform;
- public Transform headTransform;
- public Vector3 teleportReticleOffset;
- public LayerMask teleportMask;
- private bool shouldTeleport;
- private float fadeDuration = 2f;
- private GameObject collidingObject;
- private GameObject objectInHand;
- // Update is called once per frame
- void Update () {
- if (GetTeleport())
- {
- print("Teleport " + handType);
- if (Ray == null) {
- Ray = Instantiate(RayPrefab, this.transform.position, Quaternion.identity);
- reticle = Instantiate(teleportReticlePrefab);
- teleportReticleTransform = reticle.transform;
- }
- if (Ray != null) {
- RaycastHit hit;
- Ray.transform.position = gameObject.transform.position;
- Ray.transform.rotation = gameObject.transform.rotation;
- Debug.Log("Raycast");
- if (Physics.Raycast(new Ray (Ray.transform.position, Ray.transform.rotation*Vector3.forward), out hit, 100)) {
- hitPoint = hit.point;
- reticle.SetActive(true);
- teleportReticleTransform.position = hitPoint + teleportReticleOffset;shouldTeleport = true;
- Debug.Log(hitPoint);
- }
- else {
- reticle.SetActive(false);
- }
- }
- }
- if(GetTeleportUp()) {
- Destroy(Ray);
- Destroy(reticle);
- if(shouldTeleport) {
- Teleport();
- }
- }
- if (GetGrabDown()) {
- Debug.Log("grabbed");
- if(collidingObject) {
- GrabObject();
- }
- }
- if(GetGrabUp()) {
- Debug.Log("released");
- if(objectInHand) {
- ReleaseObject();
- }
- }
- }
- private void FadeToBlack() {
- SteamVR_Fade.Start(Color.black, 0f);
- }
- private void FadeFromBlack() {
- SteamVR_Fade.Start(Color.black, 0f);
- SteamVR_Fade.Start(Color.clear, fadeDuration);
- }
- public bool GetTeleport() {
- return teleportAction.GetState(handType);
- }
- public bool GetTeleportUp() {
- return teleportAction.GetStateUp(handType);
- }
- public bool GetGrabDown() {
- return grabAction.GetStateDown(handType);
- }
- public bool GetGrabUp() {
- return grabAction.GetStateUp(handType);
- }
- private void Teleport()
- {
- FadeToBlack();
- shouldTeleport = false;
- reticle.SetActive(false);
- Vector3 difference = cameraRigTransform.position - headTransform.position;
- difference.y = 0;
- cameraRigTransform.position = hitPoint + difference;
- Invoke("FadeFromBlack", fadeDuration);
- }
- private void SetCollidingObject(Collider colObj) {
- if (collidingObject || !colObj.GetComponent<Rigidbody>()) {
- return;
- }
- collidingObject = colObj.gameObject;
- }
- public void OnTriggerEnter(Collider other) {
- SetCollidingObject(other);
- }
- public void OnTriggerStay(Collider other) {
- SetCollidingObject(other);
- }
- public void OnTriggerExit(Collider other) {
- if (!collidingObject) {
- return;
- }
- collidingObject = null;
- }
- private void GrabObject() {
- objectInHand = collidingObject;
- collidingObject = null;
- var joint = AddFixedJoint();
- joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
- }
- private FixedJoint AddFixedJoint() {
- FixedJoint fx = gameObject.AddComponent<FixedJoint>();
- fx.breakForce = 20000;
- fx.breakTorque = 20000;
- return fx;
- }
- private void ReleaseObject() {
- if (GetComponent<FixedJoint>())
- {
- GetComponent<FixedJoint>().connectedBody = null;
- Destroy(GetComponent<FixedJoint>());
- objectInHand.GetComponent<Rigidbody>().velocity = controllerPose.GetVelocity();
- objectInHand.GetComponent<Rigidbody>().angularVelocity = controllerPose.GetAngularVelocity();
- }
- objectInHand = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement