How can I detect the color that the ball has collided with?
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.
Answer by Bill9009 · May 30, 2017 at 10:04 AM
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.
Answer by Outsid3 · May 30, 2017 at 10:25 AM
@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[i];
}
for (int i =0; i< values.Length; i++) {
Image newWedge = Instantiate(wedgePrefab) as Image;
newWedge.transform.SetParent(transform, false);
newWedge.color = wedgeColors[i];
newWedge.fillAmount = values[i] / total;
newWedge.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, zRotation));
zRotation -= newWedge.fillAmount * 360f;
}
}
Answer by Bill9009 · May 30, 2017 at 12:01 PM
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[i];
}
for (int i = 0; i < values.Length; i++)
{
Image newWedge = Instantiate(wedgePrefab) as Image;
newWedge.transform.SetParent(transform, false);
newWedge.color = wedgeColors[i];
newWedge.fillAmount = values[i] / total;
f1.Add(zRotation);
f2.Add(zRotation - newWedge.fillAmount * 360);
imageKey.Add(f1[i], 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[i] && f <= f1[i])//You might need to switch the signs on these around
imageKey.TryGetValue(f1[i], out im);
}
private void OnCollisionEnter2D(Collision col)
{
RetImage(transform.rotation.z, currentIm);
}
Your answer
Follow this Question
Related Questions
How to use a function from another script 1 Answer
Checking for a gameobject at a position 1 Answer
My main character isn't being able to colide with the objects. 1 Answer
How in the world do I display GameObjects from an array in an equal y distance to eachother 2 Answers
why isnt my code working, 2 Answers