summaryrefslogtreecommitdiff
path: root/HoldableObject.cs
blob: 3bfbfc508638d2964a73bce56c1eb772e543c1f3 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using UnityEngine;

public class HoldableObject : MonoBehaviour
{
	public Transform holder;

	public Vector3 holdingPosition;

	public Vector3 holdingRotation;

	public Vector3 holdingUpRotation;

	[HideInInspector]
	public Rigidbody rig;

	[HideInInspector]
	public Transform leftHandPos;

	[HideInInspector]
	public Transform rightHandPos;

	[HideInInspector]
	public PID pid;

	[HideInInspector]
	public Vector2 swingAngles;

	public float swingSpring;

	public Vector2 twistAngles;

	public float twistSpring;

	public bool isHeld;

	private DragHandler dragHandler;

	public LayerMask mask;

	private void Awake()
	{
		pid = GetComponent<PID>();
		rig = GetComponent<Rigidbody>();
		RightHandPos componentInChildren = GetComponentInChildren<RightHandPos>();
		if ((bool)componentInChildren)
		{
			rightHandPos = componentInChildren.transform;
		}
		LeftHandPos componentInChildren2 = GetComponentInChildren<LeftHandPos>();
		if ((bool)componentInChildren2)
		{
			leftHandPos = componentInChildren2.transform;
		}
		dragHandler = GetComponent<DragHandler>();
		mask = LayerMask.GetMask("Map");
	}

	private void Update()
	{
		if (isHeld)
		{
			dragHandler.dragAmount = 1f;
		}
		else
		{
			dragHandler.dragAmount = 0f;
		}
	}

	private void FixedUpdate()
	{
		if (!isHeld)
		{
			Hover();
		}
	}

	private void Hover()
	{
		RaycastHit hitInfo = default(RaycastHit);
		Ray ray = new Ray(base.transform.position, Vector3.down);
		Physics.Raycast(ray, out hitInfo, 1.5f, mask);
		Vector3 vector = new Vector3(0f, 0f, 0f);
		vector.y = 1f - hitInfo.distance;
		rig.velocity = Vector3.Lerp(rig.velocity, vector * 2f, Time.fixedDeltaTime * 100f);
		rig.angularVelocity = Vector3.Lerp(rig.angularVelocity, Vector3.up * 5f, Time.deltaTime * 100f);
	}
}