STEAMVR Simple operation through code

Source: Internet
Author: User

Control of the left hand

Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;
[Requirecomponent (typeof (Steamvr_trackedobject))]
public class Leftss:monobehaviour {
Steamvr_trackedobject Trackedobj;
Steamvr_controller.device Device;
Public Transform sphere;//Reset feature
void Awake () {
Trackedobj = getcomponent<steamvr_trackedobject> ();
}
void Fixedupdate () {
device = Steamvr_controller.input ((int) trackedobj.index);
if (device. Gettouch (SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log ("Touch Trigger");
}
if (device. Gettouchdown (SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log ("Touchdown Trigger");
}
if (device. Gettouchup (SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log ("TouchUp Trigger");
}
Drag the object into the Reset function
if (device. Getpressup (SteamVR_Controller.ButtonMask.Touchpad)) {
Sphere.transform.position = Vector3.zero;
Sphere. Getcomponent<rigidbody> (). Velocity = Vector3.zero;
Sphere. Getcomponent<rigidbody> (). angularvelocity = Vector3.zero;
}
}
void Ontriggerstay (Collider collider)
{
Item Crawl
if (device. Gettouch (SteamVR_Controller.ButtonMask.Trigger)) {
Collider.attachedRigidbody.isKinematic = true;
Collider.gameObject.transform.SetParent (Gameobject.transform);
}
Goods separation
if (device. Gettouchup (SteamVR_Controller.ButtonMask.Trigger)) {
Collider.attachedRigidbody.isKinematic = false;
Collider.gameObject.transform.SetParent (NULL);
Tossobject (Collider.attachedrigidbody);
}
}
Goods throw
void Tossobject (Rigidbody rigidbody)
{
Transform orgin = Trackedobj.origin? TrackedObj.origin:trackedObj.transform.parent;
if (orgin!= null) {
Rigidbody.velocity = Orgin. Transformvector ((device.velocity));
Rigidbody.angularvelocity = Orgin. Transformvector (device.angularvelocity);
} else
{
rigidbody.velocity = device.velocity;
rigidbody.angularvelocity = device.angularvelocity;
}
rigidbody.velocity = device.velocity;
rigidbody.angularvelocity = device.angularvelocity;
}
}

Control of the right hand


Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;
[Requirecomponent (typeof (Steamvr_trackedobject))]
public class Rightss:monobehaviour {
The tracking device, that's our handle.
Steamvr_trackedobject Trackedobj;
Get input from a handle
Steamvr_controller.device Device;
Fixed joint
Fixedjoint Fixedjoint;
A rigidbody located on a handle
Public Rigidbody Rigidbodyattachpoint;
public Transform Sphere;
void Awake () {
Trackedobj = getcomponent<steamvr_trackedobject> ();
}
void Fixedupdate () {
device = Steamvr_controller.input ((int) trackedobj.index);
Resetting features
if (device. Getpressup (SteamVR_Controller.ButtonMask.Touchpad)) {
Position
Sphere.transform.position = Vector3.zero;
Speed
Sphere. Getcomponent<rigidbody> (). Velocity = Vector3.zero;
Angular Velocity
Sphere. Getcomponent<rigidbody> (). angularvelocity = Vector3.zero;
}
}
void Ontriggerstay (Collider collider)
{
if (Fixedjoint = = null && device. Gettouch (SteamVR_Controller.ButtonMask.Trigger)) {
To add a joint component to the instantiated object, the position of the connection is this rigidbody
Fixedjoint = collider.gameobject.addcomponent<fixedjoint> ();
Fixedjoint.connectedbody = Rigidbodyattachpoint;
else if (fixedjoint!= null && device. Gettouchup (SteamVR_Controller.ButtonMask.Trigger)) {
Get the game object on the joint
Gameobject go = fixedjoint.gameobject;
Get a Rigidbody
Rigidbody rigidbody = go. Getcomponent<rigidbody> ();
Destroyed
Object.destroy (Fixedjoint);
Set to NULL
Fixedjoint = null;
Tossobject (Rigidbody);
}
}
void Tossobject (Rigidbody rigidbody)
{
Transform orgin = Trackedobj.origin? TrackedObj.origin:trackedObj.transform.parent;
if (orgin!= null) {
Rigidbody.velocity = Orgin. Transformvector ((device.velocity));
Rigidbody.angularvelocity = Orgin. Transformvector (device.angularvelocity);
} else {
rigidbody.velocity = device.velocity;
rigidbody.angularvelocity = device.angularvelocity;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.