** Hello, I'm Shiyun xinaini. Today is my first time to write an article. What's wrong with this article? Welcome to point out that the question I want to share with you today is: How can I get the sliding direction of the mouse on the pc and the finger on the andriod mobile platform in unity3D?
Because the platform is different, so the writing is different, let's not say much here, just code it: **
1. Acquiring the sliding direction of the mouse on the pc side in unity3D
using UnityEngine;
using System.Collections;
//Mouse sliding direction
public enum SlideDir
{
right,//Towards the right
left,//Towards the left
top,//Upward
bottom,//down
nullpoint//Directionless
}
public class GameControl : MonoBehaviour {
public SlideDir dir = SlideDir.nullpoint;//Define a common variable of enumeration type with an initial value of undirected
void Update(){
Move();
}
//Detecting the direction of mouse sliding
private SlideDir CheckDir(){
if (Input.GetMouseButtonUp(0))
{
//print ("the position when the left mouse button is pressed:" + DownPosition);
print ("When the left mouse button is raised:" + Input.mousePosition);
Vector3 offest =Input.mousePosition - DownPosition;//local variable
print ("The deviation of the left mouse button from press to lift:" + offest);
//Detection level
if (Mathf.Abs (offest.x) > 100 && Mathf.Abs (offest.x) >= Mathf.Abs (offest.y))
{
if (offest.x < 0)
dir = SlideDir.left;
else
dir = SlideDir.right;
}
//Detection Verticality
if (Mathf.Abs (offest.y) > 100 && Mathf.Abs (offest.x) < Mathf.Abs (offest.y))
{
if (offest.y > 0)
dir = SlideDir.top;
else
dir = SlideDir.bottom;
}
//Directionless
if (Mathf.Abs (offest.x) <= 100 && Mathf.Abs (offest.y) <= 100)
dir = SlideDir.nullpoint;
}
return dir;
}
private Vector3 DownPosition;
private void Move()
{
if (Input.GetMouseButtonDown (0))
{
DownPosition = Input.mousePosition;
print ("When the left mouse button is pressed:" + DownPosition);
}
if (Input.GetMouseButtonUp (0) == false)//Exit as long as there is no mouse-up action
return;
SlideDir dir = CheckDir ();//Call the CheckDir () function to get directions
}
}
//In the above code, I used Input. GetMouseButtonDown.(0)Method: To check whether the left mouse button is pressed or not, and if the mouse is pressed, it will be stored as soon as it is pressed.
The former position vector DownPosition = Input.mousePosition; then use Input.GetMouseButtonUp(0) to detect whether the left mouse button is raised or not; once raised, the deviation Vector 3 of Fest = Input.mousePosition-DownPosition is obtained; finally, the deviation vector determines the sliding direction of the mouse; finally, the results are as follows.
Note: Although this method can run effectively on pc, it is ineffective on andriod mobile platform. Because some methods are different, how to get the sliding direction of fingers on andriod mobile platform?
See the following code:
2. Obtaining the sliding direction of finger on andriod mobile platform
using UnityEngine;
using System.Collections;
//Mouse sliding direction
public enum SlideDir
{
right,//Towards the right
left,//Towards the left
top,//Upward
bottom,//down
nullpoint//Directionless
}
public class GameControl : MonoBehaviour {
public SlideDir dir = SlideDir.nullpoint;//Define a common variable of enumeration type with an initial value of undirected
void Update(){
Move1();
}
//Detecting the direction of finger sliding (mobile device)
private SlideDir CheckDir1(){
if (Input.GetTouch(0).phase==TouchPhase.Ended) //Finger lift
{
Vector2 offest =Input.GetTouch(0).position -TouchPosition;//local variable
//print ("the deviation between the left mouse button pressing and lifting:" + offest);
//Detection level
if (Mathf.Abs (offest.x) > 100 && Mathf.Abs (offest.x) >= Mathf.Abs (offest.y))
{
if (offest.x < 0)
dir = SlideDir.left;
else
dir = SlideDir.right;
}
//Detection Verticality
if (Mathf.Abs (offest.y) > 100 && Mathf.Abs (offest.x) < Mathf.Abs (offest.y))
{
if (offest.y > 0)
dir = SlideDir.top;
else
dir = SlideDir.bottom;
}
//Directionless
if (Mathf.Abs (offest.x) <= 100 && Mathf.Abs (offest.y) <= 100)
dir = SlideDir.nullpoint;
}
return dir;
}
private Vector2 TouchPosition;
private void Move1()
{
if (Input.GetTouch(0).phase==TouchPhase.Began)
{
TouchPosition = Input.GetTouch (0).position;//The position of the finger when it begins to touch
}
if (Input.GetTouch(0).phase!=TouchPhase.Ended )//Exit as long as there is no finger lift
return;
SlideDir dir = CheckDir1 ();
}
}
**In the above code, I'm not using it. Input.GetMouseButton
Down(0) method uses Input. GetTouch (0). phase= TouchPhase. Began to detect whether a finger is pressed. If a finger is pressed, the current touch position vector TouchPosition = Input.GetTouch (0).position is stored, and then Input. GetTouch (0). phase= Touchphase. Ended is used to detect whether a finger is raised. As soon as the finger is raised, the deviation Vecto of the position of the finger is obtained. R2 offest = Input.GetTouch(0). position-TouchPosition. Finally, more deviation vectors determine the sliding direction of the mouse. Finally, the running results are tested on the andriod device by the author and are effective.
This is the end of the article. If you have any questions, please leave me a message. (Actually, I am also a beginner, sharing this article in the hope of helping more beginners.) * *