Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Winnin position.

Discussion in 'Scripting' started by Funwill, Sep 2, 2023.

  1. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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?
     

    Attached Files:

  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,862
    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
     
  3. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    Yes, stuck at identifying wich space is the winner.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,862
    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.
     
    angrypenguin likes this.
  5. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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...
     
  6. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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:

    Code (CSharp):
    1. private int GetSliceIndex()
    2.     {
    3.         // Get the Handle game object's position relative to the wheel
    4.         Vector3 handlePosition = cylinderTransform.InverseTransformPoint(Handle.transform.position);
    5.  
    6.         // Calculate the angle between the needle and the axis on the edge between two spots on the wheel
    7.         float needleAngle = Mathf.Atan2(handlePosition.y, handlePosition.z) * Mathf.Rad2Deg;
    8.  
    9.         // Normalize the angle to be between 0 and 360 degrees
    10.         if (needleAngle < 0)
    11.         {
    12.             needleAngle += 360f;
    13.         }
    14.  
    15.         // Calculate the full angle of revolution between the axis and the needle
    16.         float fullAngle = 360f - needleAngle;
    17.  
    18.         // Calculate the slice index based on the full angle and the number of spots
    19.         float sliceAngle = 360f / 9f; // Assuming 9 slices
    20.         int sliceIndex = Mathf.FloorToInt(fullAngle / sliceAngle);
    21.         return sliceIndex;
    22.     }
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    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... :cool:
     
  8. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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...
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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.

    Doing this should help you answer these types of questions:

    - 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)

    Knowing this information will help you reason about the behavior you are seeing.

    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://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    If you just need a functioning spinner wheel, here's one attached as a .unitypackage file.
     

    Attached Files:

  11. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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...
     
    Last edited: Sep 4, 2023
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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.
     
  13. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    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:

    Code (CSharp):
    1. public class TipCollisionHandler : MonoBehaviour
    2. {
    3.     public int sliceIndex = -1;
    4.  
    5.     private void Update()
    6.     {
    7.         // Cast a ray from the tip position along the x-axis
    8.         Ray ray = new Ray(transform.position, transform.right);
    9.         RaycastHit hit;
    10.  
    11.         // Check if the ray hits any objects with the "Slice" tag
    12.         if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Slice")))
    13.         {
    14.             // Get the slice index from the Slice component attached to the collided slice object
    15.             Slice slice = hit.collider.gameObject.GetComponent<Slice>();
    16.             if (slice != null)
    17.             {
    18.                 sliceIndex = slice.SliceIndex;
    19.             }
    20.         }
    21.         else
    22.         {
    23.             // Reset the slice index if no collision is detected
    24.             sliceIndex = -1;
    25.         }
    26.     }
    27. }
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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.
     
  15. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    still not understanding where your issue completely lies, please answer the following:
    1. Are the supposed slices each their own gameObject?
    2. Did you create each slice, via Blender or other modeling program?
    3. Will these slices ever change in size(as per more slices on a wheel)?
     
    Last edited: Sep 4, 2023
  16. Funwill

    Funwill

    Joined:
    Nov 10, 2018
    Posts:
    128
    Thxs folks, i'm just learnin codin, as well as all the unity features required to a 3D RPG, as well as many other things, so that i canot focus on a sole element, buildin my game as much as i'm playin with it with the discovery of its world and mechanics. Got no time pressure, but sure i take it easy. And learnin a lot nonetheless, even with codin, even though it could be slow. And my world is takin shape with every day passin by.

    Now i got this issue with the spinwheel, its slices bein the unchangin childs images of an empty game object bein child of a canvas. Tried to add mesh filter, renderer and collider to the images, but without efects... The whole spinwheel comes from the fortune spinwheel asset (https://assetstore.unity.com/packages/tools/gui/fortune-spin-wheel-pro-171388) with wich i've tinkered a bit, addin and deletin some parts, tryin to adapt it for 3D.