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