Hi!
I’m working on a little game where you rotate a wheel with diferent colored wedges and you must catch the incoming balls with the corresponding wedge color.
The problem here is I have “dynamic” wedges, by that I mean that it can change the number of wedges and the portion of the wheel that they represent:


And I don’t know how to detect which wedge the ball has collided, because the collider gets the full circle and not the wedge (the wedges are a filled type UI image of a circle) .
Any idea on how to detect which color the ball has collided?
Thank you very much.
ps. excuse my english.
Alright first I need the script that allows the change of wedge color and portions. What you’ll have to do is OnCollisionEnter2D() get the rotation of the gameObject and with the information of how what sides are which color you can calculate the color.
@Bill9009 Sure, here is the script:
public float[] values;
public Image wedgePrefab;
public Color[] wedgeColors;
public float angle;
void Start() {
MakeGraph();
}
void MakeGraph() {
float total = 0f;
float zRotation = 0f;
for (int i = 0; i < values.Length; i++) {
total += values*;*
}
for (int i =0; i< values.Length; i++) {
Image newWedge = Instantiate(wedgePrefab) as Image;
newWedge.transform.SetParent(transform, false);
newWedge.color = wedgeColors*;*
newWedge.fillAmount = values / total;
newWedge.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, zRotation));
zRotation -= newWedge.fillAmount * 360f;
}
}
This might work:
public float[] values;
public Image wedgePrefab;
public Color[] wedgeColors;
public float angle;
public static Image currentIm;
Dictionary<float, Image> imageKey;
List<float> f1 = new List<float>();
List<float> f2 = new List<float>();
void Start()
{
MakeGraph();
}
void MakeGraph()
{
float total = 0f;
float zRotation = 0f;
for (int i = 0; i < values.Length; i++)
{
total += values*;*
}
for (int i = 0; i < values.Length; i++)
{
Image newWedge = Instantiate(wedgePrefab) as Image;
newWedge.transform.SetParent(transform, false);
newWedge.color = wedgeColors*;*
newWedge.fillAmount = values / total;
f1.Add(zRotation);
f2.Add(zRotation - newWedge.fillAmount * 360);
imageKey.Add(f1*, newWedge);*
newWedge.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, zRotation));
zRotation -= newWedge.fillAmount * 360f;
}
}
void RetImage(float f, ref Image im)
{
for (int i = 0; i < imageKey.Count; i++)
if (f > f2 && f <= f1*)//You might need to switch the signs on these around*
imageKey.TryGetValue(f1*, out im);*
}
private void OnCollisionEnter2D(Collision col)
{
RetImage(transform.rotation.z, currentIm);
}