Math Issue With Menu Wheel

I’m trying to make a wheel that you can cycle through and whatever object is selected is at the center and goes up a little. I had it working perfectly fine, but it turns out if I add some variety to the text lengths it ruins everything. The objects are spaced apart based on how long the text string is… Here’s my script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Linq;

public class TextWheelFlooper : MonoBehaviour
{
    public List<string> texts = new List<string>(1);
    public List<TextMeshProUGUI> textPros;
    public List<TextMover> textos;

    [SerializeField]private int numberCount = 0;
    private Transform textWheelPosition;
    public GameObject textObj;
    public float lengthMultiplier = 10;
    private Vector3 posGoal;

    void Start()
    {
        posGoal = transform.position;
        textWheelPosition = this.transform;
        for (int i = 0; i < texts.Count; i++)
        {
            textPros.Add(Instantiate(textObj.GetComponent<TextMeshProUGUI>(),transform.position, Quaternion.identity, transform));
            textos.Add(textPros[i].GetComponent<TextMover>());
        }
       
        for (int i = 0; i < texts.Count; i++)
        {
            textPros[i].text = texts[i];
        }

        UpdateWheel();
    }

    private void Update()
    {
        if(transform.position.x != posGoal.x || transform.position.y != posGoal.x)
        {
            transform.position = Vector3.Lerp(transform.position, posGoal, 1 * Time.deltaTime);
        }
    }
    public void NumberFlip( int num)
    {
        if (num > 0)
        {
            for (int i = 0; i < textos.Count; i++)
            {
                if (i != textos.Count)
                {
                    textos[i].textPosGoal += new Vector3(-25 - (texts[i].Length * lengthMultiplier) * 2, 0, 0);
                }
            }
        }
        else
        {
            for (int i = 0; i < textos.Count; i++)
            {
                textos[i].textPosGoal += new Vector3(25 +(texts[i].Length * lengthMultiplier) * 2, 0, 0);
            }
        }
        numberCount += num;
       for (int i = 0; i < texts.Count; i++)
        {
            if(numberCount != i)
            {
                textos[i].textPosGoal = new Vector3(textos[i].textPosGoal.x, transform.position.y, transform.position.z);
            }
            else
            {
                textos[i].textPosGoal = new Vector3(textos[i].textPosGoal.x, transform.position.y + 10, transform.position.z);
            }
        }
    }

    public void UpdateWheel()
    {
       
        textPros[numberCount].transform.position = new Vector3(transform.position.x, transform.position.y + 10, transform.position.z);
        float previousPosX = textPros[numberCount].transform.position.x + texts[numberCount].Length * lengthMultiplier;

        for (int i = 0; i < textPros.Count; i++)
        {
            if (i > numberCount)
            {
                textPros[i].transform.position = new Vector3(previousPosX + (25 + (texts[i].Length * lengthMultiplier)), transform.position.y, transform.position.z);
                previousPosX = textPros[i].transform.position.x + texts[i].Length * lengthMultiplier;
            }
            else if(i < numberCount)
            {
                textPros[i].transform.position = new Vector3(previousPosX - (25 + (texts[i].Length * lengthMultiplier)), transform.position.y, transform.position.z);
                previousPosX = textPros[i].transform.position.x + texts[i].Length * lengthMultiplier;
            }
        }
       
    }
}

If all the text objects are uniform it works fine. The texts_.Length * lengthMultiplier is supposed to compensate if the strings are longer or shorter, but it doesn’t work as expected. If the strings are too short they move too slow, and if they’re too long they move too fast…_
I plan to eventually use this for images as well, so I need to get it working so no matter what size the objects are, they end up in the same position and line up neatly.
I’m at my wits end, could someone help me out?
Thanks

@Mashimaro7

So is this based on UGUI or are you only using typical GameObjects?

Image wouldn’t hurt either… many of this kind of questions are sort of puzzles… do you actually have a wheel or do you simply mean you have a scroller that scrolls content left/right or up/down and maybe loops around?

Oh, no, it’s not a wheel, I just don’t really know what to call it XD

It’s UGUI, here’s a picture of what it looks like,

But then when I scroll it 3 times


I could just be overthinking, I tried removing the whole length equation by setting the multiplier to 0 and increased the base number(the 25s in the equation), it looks fine, just shorter strings are kinda far apart and longer ones can overlap.

The UI element you’re building is often called a “Carousel”. This may help you google some existing implementations for your reference.

(p.s. that’s a good song!)

1 Like

I believe this is an up hill battle for you as depending on font the length of some single characters would not match your text length.

A working solution would be to not even use the UI and instead make your own font as images in where all character’s canvas sizes are the same implemented as sprite renderers . This way you know exactly how long each character is.

1 Like

@Mashimaro7

I don’t know why you try to combine transforms with UI stuff. Simply use RectTransform for all UGUI stuff. Then you can also use RectTransform to contain texts to certain width (use TextMeshPro UGUI component). If you don’t want this, then you can still get UI element dimensions from RectTransform rect, so you can place elements side by side.

There are several free scripts for UGUI that handle this (supposedly, haven’t used). Check unity UI extensions project, it might have one too and asset store too might have some free ones. At least take a look at those and you get the idea.

1 Like

Ahh, I always wondered what to call that. Thanks. Also I like the song too, but I messed up the lyrics XD

I actually wasn’t aware you had to access the RectTransform separately, I thought the two were intertwined lol. That actually solved my problem, no need for complicated maths, i can just use the rect size. Thanks

1 Like