-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDie.cs
More file actions
95 lines (81 loc) · 2.91 KB
/
Die.cs
File metadata and controls
95 lines (81 loc) · 2.91 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
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
89
90
91
92
93
94
95
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Die : MonoBehaviour
{
private bool _hitSomething;
private Rigidbody _diceRb;
[Header("Movement")]
public float force;
public float resetSpeed = 3f;
[Header("Visuals")]
public GameObject model;
[Header("Scripts")]
public FaceHandler faceHandler;
public int HitCount { get; set; }
public RaycastHit HitData { get; set; }
public int? Upface { get; private set; }
public bool LandedWithUpFace { get; set; }
// Start is called before the first frame update
void Start()
{
_diceRb = model.GetComponent<Rigidbody>();
}
public void Throw(Vector3 targetPosition)
{
var randomFloat = Random.Range(-0.2f, 0.2f);
var offset = new Vector3(randomFloat, randomFloat, 0);
_diceRb.isKinematic = false;
_diceRb.useGravity = true;
var normalisedValue = targetPosition - (transform.position + offset) + (Vector3.up * 0.6f);
_diceRb.AddForce(normalisedValue * force);
}
private void Update()
{
if (Physics.Raycast(model.transform.position, Vector3.down, out var hit, 0.15f))
{
HitData = hit;
_hitSomething = true;
HitCount++;
if (_diceRb.linearVelocity.magnitude <= 0.01f && !LandedWithUpFace)
{
FreezePhysics();
var upFace = faceHandler.CheckFaceUp();
Upface = upFace;
LandedWithUpFace = true;
}
}
}
public void FreezePhysics()
{
_diceRb.isKinematic = true;
_diceRb.useGravity = false;
_diceRb.linearVelocity = Vector3.zero;
_diceRb.angularVelocity = Vector3.zero;
}
public bool ResetPosition()
{
FreezePhysics();
Upface = null;
LandedWithUpFace = false;
HitCount = 0;
_hitSomething = false;
var targetStartRot = Quaternion.Euler(Vector3.zero);
if (Quaternion.Angle(model.transform.rotation, targetStartRot) > 0.5f ||
Vector3.Distance(model.transform.position, transform.position) > 0.01f)
{
model.transform.rotation = Quaternion.Slerp(model.transform.rotation, targetStartRot, resetSpeed * Time.deltaTime);
model.transform.position = Vector3.Lerp(model.transform.position, transform.position, resetSpeed * Time.deltaTime);
return false;
}
model.transform.rotation = targetStartRot;
model.transform.position = transform.position;
_diceRb.isKinematic = false;
return true;
}
public void Spin()
{
var rand = Random.Range(0, 720) * Time.deltaTime;
model.transform.Rotate(Vector3.Lerp(model.transform.position, model.transform.position, 0.5f), rand);
}
}