Hello, I am really a beginner and I made a swipe/mouse class in c# unity and I was hoping that some one could give me some tip on it and explain it in baby language. Here is the class that I wrote. I can get all the thing out of the class that I need, like if there was a mouse click or a swipe and which direction the swipe was. But I was hoping to get it done with less code and cleaner code so I can learn to write better code.
public class MouseClass : MonoBehaviour {
#region Private Variables
private bool possibleMouseClick;
private bool swipeMightHappen;
private int counter = 0;
private string direction = "";
private Vector3[] mousePos = new Vector3[2];
// hoeveel stappen de swipe is
private int maxCounter = 5;
// de lengte van de stappen
private float swipeDis = 5;
#endregion
#region PublicVariables
public bool swipe;
public string finalDirection = "";
public bool mouseClick;
#endregion
void Update ()
{
MouseClick();
Swipe();
}
void Swipe()
{
if(possibleMouseClick)
{
mousePos[1] = Input.mousePosition;
#region RightSwipe
if(mousePos[1].x > mousePos[0].x + swipeDis)
{
if(direction != "Right" && counter > 0)
{
possibleMouseClick = false;
}
mousePos[0] = Input.mousePosition;
counter ++;
if(direction == "")
{
direction = "Right";
}
}
#endregion
#region LeftSwipe
if(mousePos[1].x < mousePos[0].x - swipeDis)
{
if(direction != "Left" && counter > 0)
{
possibleMouseClick = false;
}
mousePos[0] = Input.mousePosition;
counter ++;
if(direction == "")
{
direction = "Left";
}
}
#endregion
#region UpSwipe
if(mousePos[1].y > mousePos[0].y + swipeDis)
{
if(direction != "Up" && counter > 0)
{
possibleMouseClick = false;
}
mousePos[0] = Input.mousePosition;
counter ++;
if(direction == "")
{
direction = "Up";
}
}
#endregion
#region DownSwipe
if(mousePos[1].y < mousePos[0].y - swipeDis)
{
if(direction != "Down" && counter > 0)
{
possibleMouseClick = false;
}
mousePos[0] = Input.mousePosition;
counter ++;
if(direction == "")
{
direction = "Down";
}
}
#endregion
#region CompleteCounter
if(counter > maxCounter)
{
finalDirection = direction;
possibleMouseClick = false;
swipe = true;
}
#endregion
#region setPosZero
if(!swipeMightHappen)
{
mousePos[0] = Input.mousePosition;
swipeMightHappen = true;
}
#endregion
}else
{
mousePos[0] = Input.mousePosition;
counter = 0;
direction = "";
swipeMightHappen = false;
}
}
void MouseClick()
{
if (Input.GetMouseButtonDown(0))
{
possibleMouseClick = true;
swipe = false;
mouseClick = false;
finalDirection = "";
}
if (Input.GetMouseButtonUp(0))
{
if(swipe == false && possibleMouseClick == true)
{
mouseClick = true;
}
possibleMouseClick = false;
}
}
}
That you in advance :)!
↧