-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cs
More file actions
90 lines (73 loc) · 3.14 KB
/
Box.cs
File metadata and controls
90 lines (73 loc) · 3.14 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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Box : MonoBehaviour
{
private Vector3 _originalPosition;
private Quaternion _originalRotation;
private bool _dieParented;
[Header("Movement")]
public float showSpeed = 2f;
public float resetSpeed = 3f;
[Header("References")]
public GameObject mainCamera;
public Die die;
private void Start()
{
_originalPosition = transform.position;
_originalRotation = transform.rotation;
if (die == null)
{
die = FindFirstObjectByType<Die>();
if (die == null)
Debug.LogWarning("Box: no Die component found in the scene. Assign it via the Inspector.");
}
}
public bool MoveBoxToView()
{
// Parent the die to the box the moment it has landed, so it travels with the box
if (!_dieParented && die != null && die.LandedWithUpFace)
{
die.FreezePhysics();
die.model.transform.SetParent(transform, true);
_dieParented = true;
}
// Rotate the box so its forward face points toward the camera.
// Fall back to Vector3.forward as the up vector when the camera is directly above or below.
var directionToCamera = (mainCamera.transform.position - transform.position).normalized;
var upVector = (Mathf.Abs(Vector3.Dot(directionToCamera, Vector3.up)) > 0.99f) ? Vector3.forward : Vector3.up;
var targetRotation = Quaternion.LookRotation(directionToCamera, upVector);
var targetPosition = mainCamera.transform.position / 3;
var positionReached = Vector3.Distance(transform.position, targetPosition) < 0.01f;
var rotationReached = Quaternion.Angle(transform.rotation, targetRotation) < 0.5f;
if (!positionReached || !rotationReached)
{
transform.SetPositionAndRotation(
Vector3.Lerp(transform.position, targetPosition, showSpeed * Time.deltaTime),
Quaternion.Slerp(transform.rotation, targetRotation, showSpeed * Time.deltaTime));
return false;
}
return true;
}
public bool ResetPosition()
{
// Unparent the die before resetting so it can return to its own anchor
if (_dieParented && die != null)
{
die.model.transform.SetParent(null, true);
_dieParented = false;
}
var positionReached = Vector3.Distance(transform.position, _originalPosition) < 0.01f;
var rotationReached = Quaternion.Angle(transform.rotation, _originalRotation) < 0.5f;
if (!positionReached || !rotationReached)
{
transform.position = Vector3.Lerp(transform.position, _originalPosition, resetSpeed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, _originalRotation, resetSpeed * Time.deltaTime);
return false;
}
transform.position = _originalPosition;
transform.rotation = _originalRotation;
return true;
}
}