Hello everyone.
I just started learning coding and now I would love to get a hint from you pros about the following problem.
Maybe some of you know the dice game Yatzee. I’m trying to recreate that, but I’m logically stuck at the moment.
So far I can toss the dices, determine which side is up and ad then a corresponding value to a list.
Here the code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dice : MonoBehaviour
{
[SerializeField] float force = 500f;
[SerializeField] Vector3 startPosition = new Vector3();
bool isRollable = true;
Rigidbody _rigidbody;
//BoxCollider _boxCollider;
void Start()
{
transform.position = startPosition;
//_boxCollider = GetComponent<BoxCollider>();
_rigidbody = GetComponent<Rigidbody>();
_rigidbody.useGravity = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isRollable)
{
if (!_rigidbody.useGravity)
{
_rigidbody.useGravity = true;
}
float dirX = Random.Range(0f, 500f);
float dirY = Random.Range(0f, 500f);
float dirZ = Random.Range(0f, 500f);
transform.position = startPosition;
transform.rotation = Quaternion.identity;
_rigidbody.AddForce(transform.up * force);
_rigidbody.AddTorque(dirX, dirY, dirZ);
isRollable = false;
}
float _OneUp = Vector3.Angle(transform.up, Vector3.up);
float _TwoUp = Vector3.Angle(-transform.right, Vector3.up);
float _ThreeUp = Vector3.Angle(transform.forward, Vector3.up);
float _FourUp = Vector3.Angle(-transform.forward, Vector3.up);
float _FiveUp = Vector3.Angle(transform.right, Vector3.up);
float _SixUp = Vector3.Angle(-transform.up, Vector3.up);
if (_rigidbody.velocity.x <= Mathf.Epsilon && _rigidbody.velocity.y <= Mathf.Epsilon && _rigidbody.velocity.z <= Mathf.Epsilon && !isRollable)
{
if (_OneUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(1);
isRollable = true;
}
else if (_TwoUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(2);
isRollable = true;
}
else if (_ThreeUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(3);
isRollable = true;
}
else if (_FourUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(4);
isRollable = true;
}
else if (_FiveUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(5);
isRollable = true;
}
else if (_SixUp <= Mathf.Epsilon)
{
DiceNumberTextScript.numbersList.Add(6);
isRollable = true;
}
}
}
}
I’m not sure if the logic is correct, because I wanted to print the values in the list on the console, but that didn’t work well:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DiceNumberTextScript : MonoBehaviour
{
//public static int[] diceNumbers = new int[5];
public static List<int> numbersList = new List<int>();
string myNumbers;
public static TMP_Text text;
void Start()
{
text = GetComponent<TMP_Text>();
}
void Update()
{
//text.text = $"{diceNumbers[0]} , {diceNumbers[1]} , {diceNumbers[2]} , {diceNumbers[3]} , {diceNumbers[4]}";
if (Input.GetKeyDown(KeyCode.P))
{
DisplayNumbers();
}
}
void DisplayNumbers()
{
foreach (var number in numbersList)
{
myNumbers += number.ToString() + ", ";
}
Debug.Log(myNumbers);
}
}
So what is my intention?
In the end, I will need five values from the dices in a list or somewhere, where I can interpret them. So I need to check if certain conditions are true. E.g. two pairs in the list, or just one pair. Did the player toss a street (1,2,3,4,5 or 2,3,4,5,6) or did he toss (5,5,5,5,5) So you see I need to find a way to interpret the numbers in the list.
Maybe you guys could help me out? I don’t wan’t a written solution. Instead I would be very happy to some hints about the right direction. So I then can try and search more concrete stuff.
I hope this was understandable and I’m very happy to hear from you.
If I’m wrong here with my question, then I’m sorry.
Thank you all