- Home /
Difference between tap and swipe.
Hey everyone
My class makes a school project for some kind of climate contest and I have to code a Game/App for that. We have a globe wich you can turn by swiping your finger ( around y and x axis). This is my code for that:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipe : MonoBehaviour
{
float rotSpeed = 40;
public GameObject Earth; //Parent object because I have the continents seperatly
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;
Earth.transform.Rotate(Vector3.up, -rotX, Space.World);
Earth.transform.Rotate(Vector3.right, rotY, Space.World);
}
}
This works fine, but now you I have to code that you can click on a continent. I think my class wants to fill in the CO² usage of this continent. For that, I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tap : MonoBehaviour
{
public GameObject Text;
int counter = 0;
void OnMouseOver() {
if(Input.GetMouseButtonDown(0)) {
counter++;
if (counter % 2 == 1){
Text.SetActive(true);
} else {
Text.SetActive(false)
}
}
}
}
I will work on cool animations later, but now I will come to the point: The continent will also get clicked, if you want to swipe and start on the continent (I new that when I wrote the code but I didn`t have a better option). I know there are some other questions about it but I want to have a solution especially for that.
Thank you very much for helping me!
Here is a photo for you:
Answer by RadonRaph · Dec 06, 2019 at 10:51 AM
Hello @Mu_Q-Code, To make the difference between a drag and a tap you need to compare distance. Register when the player click thats postion1, Wait one frame and register mouse position, thats position2. Now compare the distance (Vector2.Distance(position1, position2)) with a thresold you define.
If the distance is greater than the thresold so its a swipe else its a tap.
Hope that help you,
Raph
Hey @RadonRaph
Thanks for the quick answer! Thats a really good idea. Does Unity have some sort of WaitForFrame Function and how do I implement that? Thanks, $$anonymous$$u_Q-Coder
Yes there is WaitForEndOfFrame but its only in corroutine i suggest you to use a bool false by default switch it true when you register position1 and if its true so you switch it to false and register position2 :)
Answer by Mu_Q-Coder · Dec 06, 2019 at 05:59 PM
Hey @RadonRaph
Thanks for the quick answer! Thats a really good idea. Does Unity have some sort of WaitForFrame Function and how do I implement that? Thanks,
Mu_Q-Coder
Answer by AlleJong · Feb 29, 2020 at 11:49 PM
I am curious too how to implement differ the dragging over an map from clicking an object in that world. Cannot find any solution, it is too complicated for an artist and non coder. Some help how to setup this code would be of great help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i Achieve mesh Extrusion? 2 Answers
Can I make a collider ignore only one other collider? 0 Answers
knockback 3d c# 1 Answer