SteamVR_TestThrow.cs
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using UnityEngine;
using System.Collections;
namespace Valve.VR.Extras
{
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class SteamVR_TestThrow : MonoBehaviour
{
public GameObject prefab;
public Rigidbody attachPoint;
public SteamVR_Action_Boolean spawn = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("InteractUI");
SteamVR_Behaviour_Pose trackedObj;
FixedJoint joint;
private void Awake()
{
trackedObj = GetComponent<SteamVR_Behaviour_Pose>();
}
private void FixedUpdate()
{
if (joint == null && spawn.GetStateDown(trackedObj.inputSource))
{
GameObject go = GameObject.Instantiate(prefab);
go.transform.position = attachPoint.transform.position;
joint = go.AddComponent<FixedJoint>();
joint.connectedBody = attachPoint;
}
else if (joint != null && spawn.GetStateUp(trackedObj.inputSource))
{
GameObject go = joint.gameObject;
Rigidbody rigidbody = go.GetComponent<Rigidbody>();
Object.DestroyImmediate(joint);
joint = null;
Object.Destroy(go, 15.0f);
// We should probably apply the offset between trackedObj.transform.position
// and device.transform.pos to insert into the physics sim at the correct
// location, however, we would then want to predict ahead the visual representation
// by the same amount we are predicting our render poses.
Transform origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
if (origin != null)
{
rigidbody.velocity = origin.TransformVector(trackedObj.GetVelocity());
rigidbody.angularVelocity = origin.TransformVector(trackedObj.GetAngularVelocity());
}
else
{
rigidbody.velocity = trackedObj.GetVelocity();
rigidbody.angularVelocity = trackedObj.GetAngularVelocity();
}
rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
}
}
}
}