- Home /
How to make One Arm Bandit?
Hello! Im currently busy with making kinda slot machine. I have a cilinder which has an animation where it rotates within 6 frames, each frame adds 60 degrees to the rotation and represents certain picture on the cilinder. What I want to do is to push one button to start spinning the disk (done) and another one to stop the spinning animation on a random frame. And it must be perfectly aligned in order to see the random result clearly. What is the best way to do so?
Answer by robertbu · May 03, 2013 at 11:09 PM
Your question gave me a good excuse to play with animations which I've only used on a superficial level. Contrary to the many UA posts on pausing animations and setting the animation time, I was not able to freeze the animation on a specific time/frame. That is, the animation state reported the correct time, but the visual did not match. So here is a brute force solution of simply setting the rotation:
var minSpinTime = 2.8;
var maxSpinTime = 3.2;
function Start() {
animation["cylanim"].wrapMode = WrapMode.Loop;
}
function Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
animation.Play();
Invoke("StopWheel", Random.Range(minSpinTime, maxSpinTime));
}
}
function StopWheel() {
animation.Stop();
transform.rotation = Quaternion.Euler(Random.Range(0,6) * 60.0, 0.0, 90.0);
}
thank you for the answer! I tried this but it works not in correct way I believe. First is that the cilinder rotates in wrong axis. in my case it is placed along Z axis, and needs to spin in X, the animation makes it spinning from frame 0 till 6, rotating from 0 - 360 degrees - I placed it this way coz it looks like you are infront of the slot machine. Picture 1 - is how everything placed, and pic2 - play mode + space is pushed a few times. It just flips different axis and doesn't spin at all.. I am super noob, maybe yu have an idea?
All the "magic" is here:
Quaternion.Euler(Random.Range(0,6) * 60.0, 0.0, 90.0)
Quaternion.Euler() takes three parameters for x, y, and z. So Let's assume that your wheel has a natural rotation of (0,0,0) (i.e. its base rotation before the button is pressed), and you want to rotate it around the 'Z' axis. You would need:
Quaternion.Euler(0.0, 0.0, Random.Range(0,6) * 60.0);
You want the "Random.Range(0,6) * 60.0" for the axis you rotate around.
Hello again! I changed a few things and it works! But still with 1 problem. The code looks like that:
var $$anonymous$$SpinTime = 2.8;
var maxSpinTime = 3.2;
function Start()
{
//animation["spin"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
}
function Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.Play();
// Invoke("StopWheel", Random.Range($$anonymous$$SpinTime, maxSpinTime));
}
if(Input.Get$$anonymous$$eyDown("z"))
{
Invoke("StopWheel", Random.Range($$anonymous$$SpinTime, maxSpinTime));
}
}
function StopWheel()
{ animation.Stop();
transform.rotation = Quaternion.Euler(Random.Range(0,6) * 60.0, 0.0, 0.0);
}
I removed 90.0 degrees that u assigned for the Z axis, and I commented out the Invoke thingy and placed it under the condition of Z-button pressed. I want all this rotation stop after Z pressed. So it does! But! $$anonymous$$inor issue is that after I stop the spinning by pushing Z everything stops AND after 2-3 seconds it flips one more time (the same axis) to another frame, seems like. I believe the function StopWheel() works and stops everything but after that probably (Random.Range($$anonymous$$SpinTime, maxSpinTime)) makes it jump to another frame. So to say, very unnatural look. Another $$anonymous$$or issue is that the animation starts right after entering the play mode, not with pushing "space". Considering this issue data, can u conclude something logical? Very big thanks to you for the attention and help! $$anonymous$$ay be one day we will make it working! :D
Ok! I made it working! I changed a few things. In order to stop the animation I used just StopWheel(), without additional Random.Range. So I push Z now and it stops correctly placed with perfect angle. Nice. Thanks! But now I want to check which segment out of 6 is shown to front. So to say, what picture person sees infront of him. I have them 6 around the wheel, as angles/frames we used respectively. So what I am currently doing - I placed 6 empty objects on the wheel from side, each of them has only collider, and I figured out that rigid body is also necessary to attach. So, each of them is attached to certain color section on my wheel. I created another empty object "hook" with character controller and I am checking which one of those stops in the middle section and collides with the object+characterController. Every empty object from the wheel gives Debug.Log("current color name"). But the problem is that after the very first collision between "hook" and one of those 6 objects it seems cannot overwrite the console to another debug.log, from different collision, that goes after next spin. So that is strange for me. Coz all my barely solid concept depends from it. If I can't check which image is shown after stop, I can assign any winning condition. And I surely will have 3 wheels and all the outcomes of each spin out of each wheel.
Is your code stopping randomly on one of the six sides or are you getting the same side each time? There are a list of ways to make this happen, but I can't think of a simple one (and I don't have time for an extended answer until this evening). Since this is a new question/issue consider opening it as a new question.