Combo mechanism for button clicker?
So I have this basic script: when you click a button you get 1 cash. But I wanted to make a combo mechanism where you get 2 cash for 1 click when two images are alligned. Anyone who could help me?,
{ public Text CashTotalText;
float Cash;
public void AddCash()
{
Cash++;
CashTotalText.text = Cash.ToString("0");
}
Answer by petur · Jul 28, 2021 at 08:15 PM
The most basic way to do it is checking wether those images are aligned or not before adding the cash.
Also depends on what you mean by aligned (on the same place? one on top of the other? one on the left or right of the other?) I am going to assume it is "on the same place or close enough".
First, you need to get a reference to said images to access and check their properties. The most basic way of achieving this is to create properties in your class that will show in the inspector, then assign them in the inspector. Since the "Transform" component of an image has the center position we can use that. If those images are in the UI, "RectTransform" would be a better option because it has more information (you can use the top of the images, or the corners, etc).
[SerializeField] private Transform image1Transform, image2Transform;
Now we substract the positions of the images, which results in a Vector3 with the distance in each axis.
Vector3 distance = image1Transform.position - image2Transform.position;
Fortunately, there is no need for us to calculate the magnitude (distance in every axis combined), as the Vector3 class has a property we can use: distance.magnitude, and compare it with a threshold (i am using "1", this is a long distance in the 3D world but in the UI could be very small depending on how it is rendered)
if (distance.magnitude < 1)
cash += 2;
So, finally:
float Cash;
[SerializeField] Transform image1Transform, image2Transform;
public void AddCash()
{
Vector3 distance = image1Transform.position - image2Transform.position;
if (distance.magnitude < 1)
Cash += 2;
else
Cash++;
CashTotalText.text = Cash.ToString("0");
}
Remember to assign those image references in the inspector. Hope it helps and encourages you to learn more about programming.
Great it works, even better if I chage the distance to 15 instead of 1. But is there a way to increase the cash if you click multiple times at a row when the two images are alligned? And then resets the cash clicker to 1 when missing it?
Your answer
Follow this Question
Related Questions
Combo cash per click 0 Answers
How to to draw texture on object using code 2 Answers
Combo in bolt 0 Answers
any triple mouse click c# script? 2 Answers
How to deal with repetetive code? 2 Answers