I’m having trouble adding strength to a dice and make him go forward and then after a certain time. And also like to know what was the face that is glued to the floor in order to then pin down the value of the dice.
Sorry for the question. Because I am a beginner in programming.
Sorry for my bad English.
using UnityEngine;
using System.Collections;
public class Dice1Script : MonoBehaviour {
public CubePlayerScript cubePlayerScript;
public float forceMove = 5.0f;
public GameObject face1;
public GameObject face2;
public GameObject face3;
public GameObject face4;
public GameObject face5;
public GameObject face6;
// Update is called once per frame
void Update () {
if (cubePlayerScript.launchedDice) {
rigidbody.AddForce(forceMove * Time.deltaTime, 0, 0);
}
if (rigidbody.velocity == Vector3.zero) {
if(Physics.Linecast(transform.position, face1.transform.position)){
Debug.Log("The dice value is 6");
}
}
}
}
2 Answers
2
- Typically you are rolling dice, you want to add force once, not every frame, and if you are adding force over multiple frames, then it should be done in FixedUpdate(), not Update. I don’t see where cubePlayerScript.launchDice gets set, so I’m not sure how you are handling force.
- Assuming you left the mass at 1.0, the amount of force you are applying is too small. Depending on your scale, typical values are for AddForce for a mass of 1.0 are in the 500 to 2000 range. Assuming 60 fps, your force is 5/60 = .083.
- You likely want to do both an AddForce() and an AddTorque() so the dice both spins and moves.
As for telling which side is up, you can use Vector3.Dot() or Vector3.Angle(). If I calculate these six values:
Vector3.Dot(transform.up, Vector3.up);
Vector3.Dot(transform.right, Vector3.up);
Vector3.Dot(transform.forward, Vector3.up);
Vector3.Dot(-transform.up, Vector3.up);
Vector3.Dot(-transform.right, Vector3.up);
Vector3.Dot(-transform.forward, Vector3.up);
The highest one will be the side that is most upfacing. If the dice are on a flat surface, the value will be near or at 1.0 for the side facing up. Note the side facing down will have a value of near or at -1.0.
It’s much better to use AddForce with a derived direction, and using a ForceMode can dramatically change how it gets “thrown”. Call this one in Start or on command, not in Update.
void ThrowDice() {
//Apply 10 units of force in the global Z-axis in one frame
rigidbody.AddForce(Vector3.forward * 10, ForceMode.Impulse);
}
ForceModes
And then check its velocity to see if it has stopped.
void Update() {
if (rigidbody.velocity.magnitude < 0.1) {
CheckFace();
}
}
Now for the fun part. Raycast from all local axes in order to see which side is touching the ground. Might want to use a tag or something for this.
rigidbody.AddForce(Vector3.forward * 10, ForceMode.Impulse); This makes the Dice always go ahead. The tendency is to stop rolling in the given time
– alexdevAoYou only need one raycast if you cast down from above the dice. This will give you the face that's up, and the opposite face is 'down'
– Habitablaba