- Home /
 
how can I change the scene after throwing a dice??
Hello.I am using a dice in my game.I want to change my scene after throwing this dice. But I can't do this properly because I couldn't find this line (Application.LoadLevel("firstLevel"); ) place in my code. After throwing dice , it has some rotation on the plane. I want to change scene aftar these rotations. My code :
public var faceValue = 0;
function OnTriggerEnter( other : Collider ) {
dieGameObject = GameObject.Find("SixSidedDie");
dieValueComponent = dieGameObject.GetComponent("DieValue");
dieValueComponent.currentValue = faceValue;
Debug.Log(faceValue);
Application.LoadLevel("firstLevel");
}
Main problem is this : with this codes when dice and plane have a collision with eachother scene is changed but I want this change happens after dice stops on the plane.
Answer by DaveA · Nov 10, 2011 at 08:04 PM
You will need to know when the dice are in motion, and when they have stopped. How are you moving the dice? In any case, in an Update function, get the velocity (and time). After a set amount of time has passed after you detect that their velocity(s) are zero, then call Application.LoadLevel.
These are moving codes :
public var currentValue = 0;
var power : float = 500.0;
function Start () {
transform.rotation=Random.rotation;
rigidbody.AddForce(Vector3(0,0,power));
}
function Update() {
 transform.Rotate(Vector3.right * Time.deltaTime);
 transform.Rotate(Vector3.up * Time.deltaTime,Space.World);
 dieTextGameObject = GameObject.Find("DieText");
 text$$anonymous$$eshComponent = dieTextGameObject.GetComponent     ("Text$$anonymous$$esh");
 
                  text$$anonymous$$eshComponent.text = currentValue.ToString();
}
function FixedUpdate () {
  rigidbody.AddTorque (Vector3.up * 1);
 
                  }
You told me -> (You will need to know when the dice are in motion, and when they have stopped) . But how can I know that ? It's unpredictable.
In Update, check rigidbody.velocity for having magnitude zero: http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html
How did you fix it? I'm having a lot of trouble with this exactly thing and I'm unable to solve this: the velocity of my rigidbody is never 0 even if the object isn't moving
That's odd, so maybe you can check if the velocity magnitude is < .0001 or something
Answer by jabad · Nov 10, 2011 at 10:30 PM
The best way to detect whether the two dice have stop moving is to put a character controller on each one. a character controller has a velocity variable that you can check whether or not that object is moving.
So if you check when the two velocities have hit zero then you can load your new level. good luck.
His code indicates rigidbody, which is why I suggested check velocity on them.
Your answer