Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mathidegroodt · Jul 28, 2021 at 06:41 PM · scripting beginnerclickbasicscombobasic programming

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");
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mathidegroodt · Jul 29, 2021 at 06:06 AM 0
Share

Wow thanks! I will try it.

avatar image mathidegroodt · Jul 29, 2021 at 06:28 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges