- Home /
want to find out what side of die is pointing up
First Post to this forum, first attempt at C# & Unity so I appreciate any/all feedback. I am trying to create a dice game and want to determine which side of the rigidbody has the highest Y directional axes and return that face value. I currently get a warning sign saying unreachable code detected and do not have any value returned for my y axes.
here is my current code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ApplyForceInRandomDirection : MonoBehaviour {
public string buttonName = "Fire1";
public float forceAmount = 10.0f;
public ForceMode forceMode;
public Rigidbody rb;
public float torqueAmount = 10.0f;
public Vector3 dieDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
dieDirection = rb.transform.TransformDirection(0,0,0);
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown (buttonName)) {
rb.AddForce (Random.onUnitSphere*forceAmount, forceMode);
rb.AddTorque (Random.onUnitSphere*torqueAmount, forceMode);
}
}
public Vector3 GetDirection(float x, float y, float z){
return transform.TransformDirection (dieDirection);
print (y);
}
}
The unreachable code error is the print(y); because it is after the return of the function.
Attach 6 empty objects as children of dice. Each object is at some distance from one of the faces of dice. When dice stops rolling, just find which empty object has highest Z position value. this way you will know which side corresponds to that empty object. you can name those objects as side numbers and parse the Name as an Int.
this is a good approach. it also works for arbitrary-sided dice like D20. for D4 you'd want to look for the lowest face.
You could do without extra objects if you just have a list of the normals of your die's sides. Calculate Dot products with Vector3.up and the biggest number you get is the side pointing up.
With a 6 sided die this is especially easy because the normals are transform.forward, transform.right etc. (Assu$$anonymous$$ you use Unity's primitive cube or some other mesh that's axis aligned )
Answer by KataSeiko · Feb 22, 2017 at 12:11 PM
With the dice, you should work with a random rotation and drop the dice from maybe 5 Unity units height onto a flat surface. In the real world, you let the dice drop from a certain height on e.g. a table. So it's best to copy this into the game world:
public GameObject dice;
public Vector3 dropLocation = new Vector3(0, 5, 0);
public float tumble = 5f;
public void RandomRotator()
{
dice.transform = dropLocation;
RigidBody rb = this.GetComponent<RigidBody>();
rb.angularVelocity = Random.insideUnitSphere * tumble;
}
You can call this e.g. from a button and let Physics do the rest.
Afterwards, you can check the angles on the transform. A rotation of -90 and 90 on X would be the first two sides, -90 and 90 on the Z would be the other two, 0 and -180 on the Y would be the last two sides..
Your "unreachable code" is what you wrote after "return". Return can be used to end functions prematurely (e.g. "if (gameOver) { return; }; ). In this case, it ends the function before you call "print".