|
I have a slot machine with 3 reels. Each reel is a simple cylinder divided into 12 x 30 degree segments, with each segment having a picture. Match the pictures and you win. When I pull the handle, I basically have a spin timer that rotates the reels until the time stops i.e The spinning and stopping all works nicely, I have code for that, but what stumps me here is, if I rotate the reel within Unity's editor, i.e I set the rotation to 90 in the editor, I see a totally different picture on the reel than to the one if the reel had been rotated in game mode when the rotation of the cylinder returned by Unity is also 90. (Or any other rotation for that matter). The value of 'currentAngle' which I get from the reels x eulerAngle don't match. In other words a different picture on the slot shows than what should according to the rotation of the wheel in the editor. I've tried almost every way in the book I can to get the angle, using Quaternions and rotating the wheel using other methods, but I cannot for the life of me get the rotation of the wheel in the editor to match that of the value returned while the game is running. I need this value so I can set the wheel's rotation to snap to 30 degree intervals so if it lands close to the picture the rotation is snapped so the picture is displayed in the middle. There is no rigidbody attached to the reel at all, it is simply the rotation of the mesh.
(comments are locked)
|
|
I don't know what's causing this discrepancy, but I would do it in a different manner: have a number rotating in module 12, multiply it by 30, convert to quaternion and set transform.rotation. It's better than using Rotate freely and try to guess the position using eulerAngles (they can show very strange angles sometimes).
var rotSpeed: float = 18; // speed in segments per second
var rollTime: float = 2.5; // time to roll until stop
var number: int; // number when reel stopped
private var angle: float = 0;
private var time: float = 0;
function Update(){
if (Input.GetKey("r")){
time = rollTime; // keep rolling at full speed while r pressed
number = 0; // number invalid while rolling
}
if (time > 0){
var t = time / rollTime; // reduce speed during rollTime
angle = (angle + t * rotSpeed * Time.deltaTime) % 12;
transform.rotation = Quaternion.Euler(angle * 30, 0, 90);
if (t < 0.2){ // if speed below 20% rotSpeed...
if (angle % 1 < 0.05){ // roll until segment position error < 5%
number = Mathf.Ceil(angle); // save number 1 to 12
time = 0;
}
} else {
time -= Time.deltaTime;
}
}
}
Thanks for the suggestion, when I get home I'll give it a bash and let you know. Yeah the angles are definately inconsistent. This might be what I need.
Jul 29 '11 at 06:05 AM
Meltdown
I forgot to mention: in my tests I used a cylinder, and rotated it 90 degrees around Z to get it in vertical position - that's why Quaternion.Euler has 90 degrees at the Z axis.
Jul 29 '11 at 05:14 PM
aldonaletto
Dude I cannot thank you enough for that script. That really helped me out of a very stuck situation. Much appreciated.
Jul 29 '11 at 07:29 PM
Meltdown
I know the feeling: I had a hard time once trying to track the angle of a Rotate'd propeller - that's why I lost my faith in eulerAngles after Rotate - you definitely can't trust in this combination.
Jul 30 '11 at 12:06 AM
aldonaletto
In Quaternions we shall trust :)
Jul 31 '11 at 07:47 AM
Meltdown
(comments are locked)
|
|
I'm guessing that in the Editor you are affecting the localRotation and this code affects the world rotation? Hi Dave, whether I use Space.World or Space.Self in my transform.rotate method, either way the angles returned in my log file or in the gui don't correspond with what they look like if I set that value in the editor. Any other ideas? This one has got me stumped :(
Jul 28 '11 at 08:17 PM
Meltdown
When the rotation is set to Space.World The x rotation in the inspector for the object when the 1 reel stops shows 300.0218 The x eulerangle for the object in my gui/logging shows 329.9782 When the rotation is set to Space.Self The x rotation in the inspector for the object when the 1 reel stops shows 301.7924 The x eulerangle for the object in my gui/logging shows 328.2076 Any idea why there is this constant discrepancy?
Jul 28 '11 at 08:20 PM
Meltdown
(comments are locked)
|

What I find useful for debugging in situations like this is to plot the values. Keep track of your own euler x, write this value and the actual euler x out to file, something like this:
(open and close the stream appropriately)
open this file in Excel, and plot a graph of the two variables against the time in the first colum. You should be able to see instantly what is going on: perhaps they are out by a factor, perhaps rotate sets the actual degrees (and not relative).
(You can make a general system for this kind of debugging: http://devmag.org.za/2011/02/09/using-graphs-to-debug-physics-ai-and-animation-effectively/ )
Thanks for taking the time to comment Herman. I thought I was just doing something noobly wrong. You're right, some in depth debugging is needed. I'll plot everything out and see if I can pick up any discrepancies.