如何获取圆盘位置
public class anjiangTouch : MonoBehaviour
{
public SteamVR_TrackedObject track;
public SteamVR_Controller.Device device;
void Start ()
{
track = GetComponent<SteamVR_TrackedObject>();
}
void Update ()
{
device = SteamVR_Controller.Input((int) track.index);
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
float angleTrue = ReturnDiretion();
if (angleTrue>45&&angleTrue<135)
{
Debug.Log("下");
}
if (angleTrue < -45 && angleTrue > -135)
{
Debug.Log("上");
}
if (angleTrue < 180 && angleTrue > 135||(angleTrue<-135&&angleTrue>-180))
{
Debug.Log("左");
}
if (angleTrue >0 && angleTrue < 45 || (angleTrue > -45 && angleTrue < 0))
{
Debug.Log("右");
}
}
}
public float ReturnDiretion()
{
Vector2 axis = device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad);
Vector3 crossA = Vector3.Cross(Vector2.right, axis);
float angle = Vector2.Angle(Vector2.right, axis);
return crossA.z > 0 ? -angle : angle;
}
}
手柄震动
//左手震动
var device1 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);
SteamVR_Controller.Input(device1).TriggerHapticPulse(2000);
// 获取左右手柄的标志
var device2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
// 设置获取手柄的震动值
SteamVR_Controller.Input(device2).TriggerHapticPulse(100);
瞬移功能的实现
写入一下脚本并将其挂载在左手手柄或者右手手柄上,并在其上添加另一个按键脚本<SteamVR_TrackedController>
using UnityEngine;
using System.Collections;
public class CatchPointer : MonoBehaviour
{
public bool active = true;
public Color color;
public float thickness = 0.002f;
public GameObject holder;
public GameObject pointer;
bool isActive = false;
public bool addRigidBody = false;
public Transform reference;
public event PointerEventHandler PointerIn;
public event PointerEventHandler PointerOut;
public Vector3 HitPoint;
Transform previousContact = null;
// Use this for initialization
void Start ()
{
holder = new GameObject();
holder.transform.parent = this.transform;
holder.transform.localPosition = Vector3.zero;
pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
pointer.transform.parent = holder.transform;
pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
BoxCollider collider = pointer.GetComponent<BoxCollider>();
if (addRigidBody)
{
if (collider)
{
collider.isTrigger = true;
}
Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
rigidBody.isKinematic = true;
}
else
{
if(collider)
{
Object.Destroy(collider);
}
}
Material newMaterial = new Material(Shader.Find("Unlit/Color"));
newMaterial.SetColor("_Color", color);
pointer.GetComponent<MeshRenderer>().material = newMaterial;
}
public virtual void OnPointerIn(PointerEventArgs e)
{
if (PointerIn != null)
PointerIn(this, e);
}
public virtual void OnPointerOut(PointerEventArgs e)
{
if (PointerOut != null)
PointerOut(this, e);
}
// Update is called once per frame
void Update ()
{
if (!isActive)
{
isActive = true;
this.transform.GetChild(0).gameObject.SetActive(true);
}
float dist = 100f;
SteamVR_TrackedController controller = GetComponent<SteamVR_TrackedController>();
Ray raycast = new Ray(transform.position, transform.forward);
RaycastHit hit;
bool bHit = Physics.Raycast(raycast, out hit);
if(previousContact && previousContact != hit.transform)
{
PointerEventArgs args = new PointerEventArgs();
if (controller != null)
{
args.controllerIndex = controller.controllerIndex;
}
args.distance = 0f;
args.flags = 0;
args.target = previousContact;
OnPointerOut(args);
previousContact = null;
}
if(bHit && previousContact != hit.transform)
{
PointerEventArgs argsIn = new PointerEventArgs();
if (controller != null)
{
argsIn.controllerIndex = controller.controllerIndex;
}
argsIn.distance = hit.distance;
argsIn.flags = 0;
argsIn.target = hit.transform;
OnPointerIn(argsIn);
previousContact = hit.transform;
}
if(!bHit)
{
previousContact = null;
}
if (bHit && hit.distance < 100f)
{
dist = hit.distance;
}
if (bHit)
{
HitPoint = hit.point;
}
if (controller != null && controller.triggerPressed)
{
pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
}
else
{
pointer.transform.localScale = new Vector3(thickness, thickness, dist);
}
pointer.transform.localPosition = new Vector3(0f, 0f, dist/2f);
}
}
随后我们实现传送(Test_Teleporter)脚本,并将其挂载在摄像CameraRig上,脚本编写如下:
public class Test_Teleporter : MonoBehaviour {
// 拿到手柄对象
public GameObject LeftController;
//修改后的射线检测脚本
private Test_LaserPointer Pointer;
// 右手柄的控制器
private SteamVR_TrackedController LeftSt;
// 事件调用函数
ClickedEventHandler ce;
// 当前的目标位置
Transform currentTransform;
// 碰撞事件
PointerEventArgs arg;
void Start()
{
Pointer = LeftController.GetComponent<Test_LaserPointer>();
LeftSt = LeftController.GetComponent<SteamVR_TrackedController>();
// 利用Laserpoint里面的事件函数来检测目标位置
Pointer.PointerIn += LeftPointIn;
// 利用Laserpoint里面的事件函数来检测目标位置
Pointer.PointerOut += LeftPointOut;
// 利用手柄控制器来检测我们的扳机事件
LeftSt.TriggerClicked += TriggerClicked;
}
// 没有检测到碰撞信息
void LeftPointOut(object sender, PointerEventArgs e)
{ // 当前位置没有
currentTransform = null;
}
// 检测到碰撞信息
void LeftPointIn(object sender, PointerEventArgs e)
{ // 设置目标位置为检测到的目标
currentTransform = e.target;
}
// 扳机事件
void TriggerClicked(object sender, ClickedEventArgs e)
{
// 如果检测到目标位置
if (currentTransform != null)
{ // 移动
this.gameObject.transform.position = new Vector3(Pointer.HitPoint.x - LeftController.transform.localPosition.x, Pointer.HitPoint.y, Pointer.HitPoint.z - LeftController.transform.localPosition.z);
}
}
}
子弹发射功能的实现
public class TrackedController_shoot :
SteamVR_TrackedController {
void Start () {
base.Start();
}
void Update () {
base.Update();
}
public override void OnTriggerClicked(ClickedEventArgs e)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.position = this.gameObject.transform.position;
go.transform.localScale = new Vector3(0.1f,0.1f,0.1f);
go.AddComponent<Rigidbody>().AddForce(this.transform.forward * 100);
go.tag = "Super";
}
}
隔空取物功能的实现
首先需要给物体添加刚体并且设置其Is Kinematic为true.然后给其写入代码如下:
// 检测手柄指向物体或离开物体
SteamVR_LaserPointer l;
// 手柄事件系统
SteamVR_TrackedController t;
Transform pointT;
GameObject currentCatch;
void Start () {
l = GetComponent<SteamVR_LaserPointer>();
l.PointerIn += PointerIn;
l.PointerOut += PointerOut;
t = GetComponent<SteamVR_TrackedController>();
t.TriggerClicked += TriggerClicked;
t.TriggerUnclicked += TriggerUnclicked;
}
void PointerIn(object sender, PointerEventArgs e)
{
if (e.target.gameObject.tag == "Super")
{
pointT = e.target;
}
}
void PointerOut(object sender, PointerEventArgs e)
{
pointT = null;
}
void TriggerClicked(object sender, ClickedEventArgs e)
{
if (pointT == null)
{
return;
}
pointT.position = this.transform.position;
pointT.gameObject.AddComponent<FixedJoint>().connectedBody = this.GetComponent<Rigidbody>();
currentCatch = pointT.gameObject;
}
void TriggerUnclicked(object sender, ClickedEventArgs e)
{
if (currentCatch == null)
{
return;
}
var device = SteamVR_Controller.Input((int)this.GetComponent<SteamVR_TrackedObject>().index);
device.TriggerHapticPulse(2800);
// 松开将速度传递给物体
currentCatch.GetComponent<Rigidbody>().velocity = device.velocity * 5;
currentCatch.GetComponent<Rigidbody>().angularVelocity = device.angularVelocity;
Destroy(currentCatch.GetComponent<FixedJoint>();
currentCatch = null;
}