# Winnin position.

Greets! folks!

Here’s my fortune spinwheel made of a sliced cylinder and a canvas child with 9 slices images. I make it spin with a click, but i’m unable to change my character data accordingly to the handle position. Nothing changes when the wheel stop.

Would anyone have a tip on how to handle, hum, that?

What’s the actual issue?

• dividing wheel into parts (Done)
• spinning wheel (Done)
• wheel stops (Done)
• identifying which space is the winner
• identifying the prize to give with the winning space
• applying the prize to the player

Yes, stuck at identifying wich space is the winner.

Math. You know the axis which you used for spinning. The other two axes are flat against the wheel, and one of those two axes should be exactly on the edge between two spots drawn on the wheel. Figure out the full angle of revolution between that axis and the needle (that’s the signed angle, plus 360, mod 360). Divide that angle by 360, and multiply by the number of spots. Round down with Mathf.FloorToInt(). That’s your slot position.

1 Like

Ooooooooooook, managed it! Well, with a lil bit of help from chatGPT, and you of course! It’s modifying the power data when the wheel stop. Though now i have to make the right slots corespond with the right gains or loss…Cause it’s all mixed up…

Well, i’ve spoken a bit too quickly… The wheel stops and it does modify character data, but there’s only two changes made, whatever the position of the handle.

here’s the method if anyone happens to pass by, willin to help:

``````private int GetSliceIndex()
{
// Get the Handle game object's position relative to the wheel
Vector3 handlePosition = cylinderTransform.InverseTransformPoint(Handle.transform.position);

// Calculate the angle between the needle and the axis on the edge between two spots on the wheel
float needleAngle = Mathf.Atan2(handlePosition.y, handlePosition.z) * Mathf.Rad2Deg;

// Normalize the angle to be between 0 and 360 degrees
if (needleAngle < 0)
{
needleAngle += 360f;
}

// Calculate the full angle of revolution between the axis and the needle
float fullAngle = 360f - needleAngle;

// Calculate the slice index based on the full angle and the number of spots
float sliceAngle = 360f / 9f; // Assuming 9 slices
int sliceIndex = Mathf.FloorToInt(fullAngle / sliceAngle);
return sliceIndex;
}
``````

A couple thoughts come to mind;

If your slices are technically gameObjects, you can set their collider to be just them, then have a triangle collider for the “determiner” and just use the trigger method of “OnStay”… Then obviously each slice having it’s own script to handle it’s own logic, preferably from a parent class to simplify things…

If they technically don’t have colliders, or you don’t wish use colliders, as the slices sizes might change or be customizable, then I would just have each slice have it’s root position dead center, and it’s forward at the top part of the rounded section. Then just determine from dead-center round how far each end is, then correlate what is perfect world up(basically the “determiner” forward(opposite from wheel)) and see if it falls within those bounds…

Or like just mentioned, at the center of round(slice) declare that as a Vector3, and just use Vector3.Distance to which one is closer to the determiner(which would probably be the easiest way)…

Or like the first mention, instead of TriggerStay, you could just have trigger enter, with only one variable of “sliceHit”, so as it spins slowly between the two nearby slices, if it never hits the second one(in a mi-nute distance), then last slice hit is the one you calculate for endResult…

Or you could also use a raycast, if slices have a collider(of their own shape), and on “wheelSpeed = 0” just raycast once from “determiner” down to center of wheel, and first slice hit is the one you calculate…

Or… lol, I mean come on… the possibilities are endless…

Canot set a collider to my slices. Mesh ones are concave without ‘is trigger’…An then i duno how to realise the collision with my tip gameobject in order to return the sliceIndex…

If you have any tip, hum…

I’m not even sure how to pick that apart, but here’s a great simple way to debug what you have so far:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

• the code you think is executing is not actually executing at all
• the code is executing far EARLIER or LATER than you think
• the code is executing far LESS OFTEN than you think
• the code is executing far MORE OFTEN than you think
• the code is executing on another GameObject than you think it is
• you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling `Debug.Log()` statements through your code to display information in realtime.

• is this code even running? which parts are running? how often does it run? what order does it run in?
• what are the names of the GameObjects or Components involved?
• what are the values of the variables involved? Are they initialized? Are the values reasonable?
• are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as `Debug.Log("Problem!",this);`

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

If your problem is with OnCollision-type functions, print the name of what is passed in!

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the `print()` function is an alias for Debug.Log() provided by the MonoBehaviour class.

If you just need a functioning spinner wheel, here’s one attached as a .unitypackage file.

Man, you just printed out so much to help me out, and i’m so grateful. I’ll sure think of the printin tool more often, but i got already a functionin spinwheel by a click on it. Trouble is, i can’t get my handle tip detect wich slice is under it, not even in debug log, and so return the result to change my character data acordingly.

Tried and retried, drillin chatGPT to experiment new codes based on rays and whatnot, but to no avail, couldn’t get the collision of my handle tip with a slice. The best i managed was getin two variables for the 9 slices…

That’s not the approach I used in the package above. I set the angle I want and then spin it.

If you want to “detect” anything, your main choices are colliders, or else do the math to back an x/y delta (the center subtracted from the radius) into an angle.

Ok, but in my game i need the wheel to spin randomly every activation, with colliders seemin the easiest option regardin my poor skills in codin and maths. But as i said, can’t make the Tip(empty game object at the tip of my needle) collide with my slices.

Tried with this code amongst others to get the slice index:

``````public class TipCollisionHandler : MonoBehaviour
{
public int sliceIndex = -1;

private void Update()
{
// Cast a ray from the tip position along the x-axis
Ray ray = new Ray(transform.position, transform.right);
RaycastHit hit;

// Check if the ray hits any objects with the "Slice" tag
{
// Get the slice index from the Slice component attached to the collided slice object
Slice slice = hit.collider.gameObject.GetComponent<Slice>();
if (slice != null)
{
sliceIndex = slice.SliceIndex;
}
}
else
{
// Reset the slice index if no collision is detected
sliceIndex = -1;
}
}
}
``````

Cool, but staring at sheets of strange code isn’t usually helpful. If you insist on going that way then as I wrote above, it is time to start debugging. See my reply in post #9 above.

If you’re unable to do this then it is time to learn what your code is attempting to do so you can fix it. Try this Step #2 below:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

If you’re not interested in learning what your code is doing to fix it, then this forum isn’t for you.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://forum.unity.com/forums/commercial-job-offering.49/

https://forum.unity.com/forums/non-commercial-collaboration.17/

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

You may consider trying your luck at ChatGPT. However, if you cannot understand the code ChatGPT makes you, nobody here can either. Remember this is YOUR game, not ours. We have our own games we’re working on.