Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"visualstudiotoolsforunity.vstuc"
]
}
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Unity",
"type": "vstuc",
"request": "attach"
}
]
}
55 changes: 55 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.gitmodules": true,
"**/*.booproj": true,
"**/*.pidb": true,
"**/*.suo": true,
"**/*.user": true,
"**/*.userprefs": true,
"**/*.unityproj": true,
"**/*.dll": true,
"**/*.exe": true,
"**/*.pdf": true,
"**/*.mid": true,
"**/*.midi": true,
"**/*.wav": true,
"**/*.gif": true,
"**/*.ico": true,
"**/*.jpg": true,
"**/*.jpeg": true,
"**/*.png": true,
"**/*.psd": true,
"**/*.tga": true,
"**/*.tif": true,
"**/*.tiff": true,
"**/*.3ds": true,
"**/*.3DS": true,
"**/*.fbx": true,
"**/*.FBX": true,
"**/*.lxo": true,
"**/*.LXO": true,
"**/*.ma": true,
"**/*.MA": true,
"**/*.obj": true,
"**/*.OBJ": true,
"**/*.asset": true,
"**/*.cubemap": true,
"**/*.flare": true,
"**/*.mat": true,
"**/*.meta": true,
"**/*.prefab": true,
"**/*.unity": true,
"build/": true,
"Build/": true,
"Library/": true,
"library/": true,
"obj/": true,
"Obj/": true,
"ProjectSettings/": true,
"temp/": true,
"Temp/": true
},
"dotnet.defaultSolution": "DiceProject.sln"
}
61 changes: 49 additions & 12 deletions Assets/Scripts/Box.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,84 @@ public class Box : MonoBehaviour
{
private Vector3 _originalPosition;
private Quaternion _originalRotation;
private bool _dieParented;

[Header("Movement")]
public float showSpeed = 2f;
public float resetSpeed = 3f;
public float resetSpeed = 3f;

[Header("References")]
public GameObject mainCamera;

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 targetRotation = Quaternion.LookRotation(-mainCamera.transform.forward);

if (transform.position != targetPosition && transform.rotation != targetRotation)
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.Lerp(transform.rotation, targetRotation, showSpeed * Time.deltaTime));
Vector3.Lerp(transform.position, targetPosition, showSpeed * Time.deltaTime),
Quaternion.Slerp(transform.rotation, targetRotation, showSpeed * Time.deltaTime));

return false;
}

return true;
}
return true;
}

public bool ResetPosition()
{
if (transform.position != _originalPosition && transform.rotation != _originalRotation)
// 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.Lerp(transform.rotation, _originalRotation, resetSpeed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, _originalRotation, resetSpeed * Time.deltaTime);

return false;
}

transform.position = _originalPosition;
transform.rotation = _originalRotation;
return true;
}
}

55 changes: 34 additions & 21 deletions Assets/Scripts/Die.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class Die : MonoBehaviour

[Header("Movement")]
public float force;
public float resetSpeed = 3f;
public float resetSpeed = 3f;

[Header("Visuals")]
public GameObject model;

[Header("Scripts")]
public FaceHandler faceHandler;

public FaceHandler faceHandler;
public int HitCount { get; set; }
public RaycastHit HitData { get; set; }
public int? Upface { get; private set; }
Expand All @@ -32,6 +32,7 @@ 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);
Expand All @@ -44,34 +45,46 @@ private void Update()
HitData = hit;
_hitSomething = true;
HitCount++;
if (Vector3.Distance(_diceRb.velocity, Vector3.zero) <= 0.01f)
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()
{
_diceRb.useGravity = false;
_diceRb.velocity = Vector3.zero;
_hitSomething = false;
LandedWithUpFace = false;
HitCount = 0;

var targetStartRot = Quaternion.Euler(Vector3.zero);
if (model.transform.rotation != targetStartRot && model.transform.position != transform.position)
{
model.transform.rotation = Quaternion.Lerp(model.transform.rotation, targetStartRot, resetSpeed * Time.deltaTime);
model.transform.position = Vector3.Lerp(model.transform.position, transform.position, resetSpeed * Time.deltaTime);

return false;
}

{
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()
Expand Down
79 changes: 53 additions & 26 deletions Assets/Scripts/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,103 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class GameController : MonoBehaviour
{
private bool _threwDie;
private bool _restartGame;
private bool _restartGame;
private bool _moveBoxComplete;
private bool _restartComplete;
private bool _restartComplete;
private bool _hasShownResult;
private string _resultMessage = "";
private GUIStyle _resultStyle;

[Header("Visuals")]
public Die die;
public Box box;
private bool _moveBoxToView;

// Start is called before the first frame update
public Box box;
private bool _moveBoxToView;
// Start is called before the first frame update
void Start()
{
_resultStyle = new GUIStyle
{
fontSize = 48,
alignment = TextAnchor.UpperCenter,
fontStyle = FontStyle.Bold
};
_resultStyle.normal.textColor = Color.white;
}

void OnGUI()
{
if (!string.IsNullOrEmpty(_resultMessage))
{
GUI.Label(
new Rect(0, Screen.height * 0.1f, Screen.width, Screen.height * 0.2f),
_resultMessage,
_resultStyle);
}
}

// Update is called once per frame
void Update()
{
var keyboard = Keyboard.current;
if (die.HitCount < 1 && !_threwDie)
{
if (Input.GetKey(KeyCode.Space))
if (keyboard != null && keyboard.spaceKey.isPressed)
{
SpinDie();
}

if (Input.GetKeyUp(KeyCode.Space))
}
if (keyboard != null && keyboard.spaceKey.wasReleasedThisFrame)
{
ThrowDie();
_threwDie = true;
}
}

if (die.LandedWithUpFace && !_moveBoxComplete)
if (die.LandedWithUpFace && !_hasShownResult)
{
_hasShownResult = true;
_resultMessage = $"You rolled a {die.Upface}!";
_moveBoxToView = true;
}

if (_moveBoxToView)
{
_moveBoxComplete = box.MoveBoxToView();
if (_moveBoxComplete)
{
_moveBoxToView = false;
}
if (_moveBoxToView)
{
_moveBoxComplete = box.MoveBoxToView();
if (_moveBoxComplete)
{
_moveBoxToView = false;
}
}

if (Input.GetKeyDown(KeyCode.R) && !_restartComplete && _moveBoxComplete)
if (keyboard != null && keyboard.rKey.wasPressedThisFrame && !_restartComplete && _moveBoxComplete)
{
_threwDie = false;
_restartGame = true;
}

if (_restartGame)
{
RestartGame();
if (_restartGame)
{
RestartGame();
}
}

private void RestartGame()
{
_restartComplete = die.ResetPosition();
_restartComplete = box.ResetPosition();
if (_restartComplete)
if (_restartComplete)
{
_moveBoxComplete = false;
_restartGame = false;
_restartComplete = false;
_hasShownResult = false;
_resultMessage = "";
_moveBoxComplete = false;
_restartGame = false;
_restartComplete = false;
}
}

Expand Down
Loading