LockToPoint.cs
1.74 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Valve.VR.InteractionSystem.Sample
{
public class LockToPoint : MonoBehaviour
{
public Transform snapTo;
private Rigidbody body;
public float snapTime = 2;
private float dropTimer;
private Interactable interactable;
private void Start()
{
interactable = GetComponent<Interactable>();
body = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
bool used = false;
if (interactable != null)
used = interactable.attachedToHand;
if (used)
{
body.isKinematic = false;
dropTimer = -1;
}
else
{
dropTimer += Time.deltaTime / (snapTime / 2);
body.isKinematic = dropTimer > 1;
if (dropTimer > 1)
{
//transform.parent = snapTo;
transform.position = snapTo.position;
transform.rotation = snapTo.rotation;
}
else
{
float t = Mathf.Pow(35, dropTimer);
body.velocity = Vector3.Lerp(body.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
if (body.useGravity)
body.AddForce(-Physics.gravity);
transform.position = Vector3.Lerp(transform.position, snapTo.position, Time.fixedDeltaTime * t * 3);
transform.rotation = Quaternion.Slerp(transform.rotation, snapTo.rotation, Time.fixedDeltaTime * t * 2);
}
}
}
}
}