htc 抓取 释放、瞬移完整代码
晋中职业技术学院 智祥明 QQ 1064270685
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class LaserPointer : MonoBehaviour {
public SteamVR_Behaviour_Pose pose_Laser;
public SteamVR_Action_Boolean teleport_Laser = SteamVR_Input.GetBooleanAction("Teleport");
public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("InteractUI");
public GameObject laserPrefab;
private GameObject laser;
private Transform laserTransform;
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;
FixedJoint joint;
private GameObject collidingObject;
private GameObject objectInHand;
private void SetColldingObject(Collider col)
{
if(collidingObject || !col.GetComponent <Rigidbody >())
{
return;
}
collidingObject = col.gameObject;
}
public void OnTriggerEnter(Collider other)
{
SetColldingObject(other);
}
public void OnTriggerStay(Collider other)
{
SetColldingObject(other);
}
public void OnTriggerExit(Collider other)
{
if (!collidingObject)
return;
collidingObject = null;
}
private void GrabObject()
{
if (collidingObject)
{
objectInHand = collidingObject;
collidingObject = null;
print(objectInHand);
joint = AddFixedJoint();
joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
}
}
private FixedJoint AddFixedJoint()
{
joint = gameObject.AddComponent<FixedJoint>();
joint.breakForce = 20000;
joint.breakTorque = 20000;
return joint;
}
private void ShowLaser(RaycastHit hit)
{
laser.GetComponent<MeshRenderer>().enabled = true;
laserTransform.position = Vector3.Lerp(pose_Laser.transform.position, hitPoint, 0.5f);
laserTransform.LookAt(hitPoint);
laserTransform.localScale = new Vector3(laserTransform.localScale.x, laserTransform.localScale.y, hit.distance);
}
// Use this for initialization
void Start () {
laser = Instantiate(laserPrefab);
laserTransform = laser.transform;
reticle = Instantiate(teleportReticlePrefab);
teleportReticleTransform = reticle.transform;
}
// Update is called once per frame
void Update () {
if (teleport_Laser.GetStateDown(pose_Laser.inputSource))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100, teleportMask))
{
hitPoint = hit.point;
ShowLaser(hit);
reticle.SetActive(true);
teleportReticleTransform.position = hitPoint + teleportReticleOffset;
shouldTeleport = true;
}
}
else
{
laser.GetComponent<MeshRenderer>().enabled = false;
reticle.SetActive(false);
}
if (teleport_Laser.GetStateUp(pose_Laser.inputSource) && shouldTeleport)
{
Teleport();
}
if (teleport.GetStateDown(pose_Laser.inputSource))
{
GrabObject();
}
if (teleport.GetStateUp(pose_Laser.inputSource))
{
if (objectInHand)
{
ReleaseObject();
}
}
}
private void Teleport()
{
shouldTeleport = false;
reticle.SetActive(false);
Vector3 difference = cameraRigTransform.position - headTransform.position;
difference.y = 0;
cameraRigTransform.position = hitPoint + difference;
}
private void ReleaseObject()
{
// 1
if (gameObject .GetComponent<FixedJoint>())
{
// 2
gameObject .GetComponent<FixedJoint>().connectedBody = null;//将FixedJiont(固定关节)组件的链接刚体属性置为空
Object.DestroyImmediate(joint);//销毁FixedJiont(固定关节)组件
// 3
objectInHand.GetComponent<Rigidbody>().velocity = pose_Laser .GetVelocity();
objectInHand.GetComponent<Rigidbody>().angularVelocity = pose_Laser .GetAngularVelocity();
}
// 4
objectInHand = null;
}
}