Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c# - How to make difference between click with moving with click without moving gameobject in unity

I have 2 2D gameobjects in unity.I want to achieve next but only with Input.GetTouch for android:

  1. When i click on first or second object---play some sound.
  2. When drag any of those game objets to each other combine them to one---without playing sound from first case.
  3. Then be able to click on this combined object---without playing sound from first case

Problem is because I dont want to play sound from first case if object is dragging. I already tried some code with couroutine but it doesnt work:

   if (Input.touchCount > 0) {
     touch = Input.GetTouch(0);
     touchPos = Camera.main.ScreenToWorldPoint(touch.position);
     RaycastHit2D hit = Physics2D.Raycast(touchPos, Camera.main.transform.forward);

     switch (touch.phase) {

     case TouchPhase.Began:
       samoKlik = true;
       if (samoKlik) {
         StartCoroutine(korotinaKlika());
       }
     }
     case TouchPhase.Moved:
       isMoved = true;

     if (!samoKlik) {
       if (jedan) {

         break;

         case TouchPhase.Ended:
           isMoved = false;
         jedan = false;

         break;
       }
question from:https://stackoverflow.com/questions/65856724/how-to-make-difference-between-click-with-moving-with-click-without-moving-gameo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to write your switch case properly. This code will not compile. The proper switch case syntac would be somethink like the one described here:

        switch (touch.phase) {
            case TouchPhase.Began:
                samoKlik = true;
                //if (samoKlik) { // This is not needed
                   StartCoroutine(korotinaKlika());
                //}
               break;
            case TouchPhase.Moved:
                isMoved = true;
                if (!samoKlik) { // samoKlik does not exist here
                    if (jedan) {
                       break;
                    }
                }
                break;
            case TouchPhase.Ended:
               isMoved = false;
               jedan = false;
               break;
       }

Since you want to fall from one conditional to another, it looks like the switch statement is not right for you.

I would suggest to try the following:

  1. Declare your variables before your logic
bool samoKlik = false;
bool isMoved = false;
bool jedan = false;
  1. Start with if/else clauses.

I can identify one for you to help you going

if (touch.phase == touchPhase.Moved && !samoKlik && jedan)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...