Unity3D learning and communication (new entry) - the pit of Android touch mode

Keywords: JoyStick Mobile

JoyStick didn't know how to publish it to the mobile phone before. Someone on the Internet said that the mouse click on the desktop is the same as the touch click on the mobile phone. Just change the WSAD key on the computer to four buttons, and the left mouse button attack to a button on the mobile phone. Other functions need to learn the touch touch touch method on the mobile terminal. At that time, I thought, change it to B Utton is very simple. Touch still doesn't care. It's hard to think about it. (do not want to use plug-ins)
In the later practical operation, I also think that there is a joystick on the left when playing mobile games. I think it's better to use a joystick or a joystick, so I searched the big guy's tutorial. How to judge the movement? Before that, the desktop uses float x = Input.GetAxis("Horizontal"); float z= Input.GetAxis("Vertical"); now the mobile uses a joystick, i.e. the movement value of the joystick in the X and Z directions is passed to float x,float z, that is Yes;

Then something bothered me for a day happened. When I used the left joystick to control the movement of the characters, and I clicked the attack button on the left to attack, there was a conflict in the Touch control. As long as I clicked the attack button, the left joystick turned to the direction of the button I clicked on the right hand. I was puzzled why I could not simultaneously click the virtual joystick on the left to control the movement of the characters, and click the button on the right to control the movement Make character attack, I use keywords to search a lot of results, there are also my questions, but there is no clear answer (now think about it, because it's too simple.. emm), which I saw about the implementation of double rocker, I think it's a bit similar, it's Touch on both sides, and then I studied for a while, forehead, complexity, I don't understand it very much, headache, I just want to click on both sides to achieve the corresponding functions normally, and when both sides click at the same time, there will be no conflict; then I have to search the Touch related information, there's really no way, no way Want to learn to also want to learn, finally or in the double rocker implementation of that code, I read it several times, suddenly thought,, no, my side is because I did not open multi Touch!!! Then I want to scroll with the mouse wheel to control the zoom of the view angle like the desktop. Finally, combined with the big guy's code, I divide the screen into three parts: the display and control of the virtual rocker on the left, the zoom control of the view angle with two fingers in the middle, and the click of the button on the right;
The code is as follows:

(1) player movement control:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class EasyTouchMove : MonoBehaviour{

    //Maximum radius of rocker movement
    public float maxRadius = 100;
    //Initialize background icon location
    public Transform moveBackPos;//Virtual rocker background position (rocker chassis)
    public Transform joy; //Virtual rocker position
    private Vector2 moveCenter;
    //Property accessor of hor,ver
    private float horizontal=0;
    private float vertical=0;
    public float Horizontal {
        get { return horizontal; }
    public float Vertical {
        get { return vertical; }
    void Start(){
        moveCenter = joy.position;
        Input.multiTouchEnabled = true;//Turn on multi touch
       void Update () {
      	horizontal = (joy.position.x - moveCenter.x)/100;
       	vertical = (joy.position.y - moveCenter.y)/100;

        if (Input.touchCount <= 0)
        if (Input.touchCount >= 1) 
            //Record the touch information of each finger
            for (int i = 0; i < Input.touchCount; i++) 
                //When the touch is in the left area, the left controls the virtual rocker to control the player's movement
                if (Input.touches[i].position.x < Screen.width*0.3f) 
		    //As long as the left area is touched, no matter how much Input.touchCount is equal to, the following will be executed
                    if (Input.touches [i].phase == TouchPhase.Began) {
                        moveCenter = Input.touches [i].position;//Moving point position is touch point position
                        moveBackPos.gameObject.SetActive (true);//Show rocker background
                        moveBackPos.position = moveCenter;//Rocker background position = moving point position
                        joy.position = moveCenter;//Rocker position = move point position
		    //Finger touch movement
                    if (Input.touches [i].phase == TouchPhase.Moved) {
                        Vector2 oppsitionVec = (Vector2)Input.touches [i].position - moveCenter;  //Moving vector
                        float length = Vector3.Magnitude(oppsitionVec);	//Vector length
                        float radius = Mathf.Clamp(length,0,maxRadius);//Limit radius = length and 0 < = length < = maxradius
                        joy.position = moveCenter + oppsitionVec.normalized * radius;
		    //Finger touch away
                    if (Input.touches [i].phase == TouchPhase.Ended) {
                        joy.position = moveCenter;
                        joy.localPosition = Vector3.zero;
                        moveBackPos.gameObject.SetActive (false);

Then modify it in the character movement control script
public EasyTouchMove etmoveļ¼›
void FixedUpdate () {
//float x = Input.GetAxis("Horizontal");
//float z = Input.GetAxis ("Vertical");
float x = etmove.Horizontal;
float z = etmove.Vertical;

(2) Visual field control

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AndoridTouchCamera : MonoBehaviour {

  private int isforward; //Zoom in / out

  //Old and new positions of two fingers
  private Vector2 oldPos1 = new Vector2();
  private Vector2 oldPos2 = new Vector2();
  private Vector2 newPos1 = new Vector2();
  private Vector2 newPos2 = new Vector2();

  //Used to determine whether to enlarge
  bool isEnlarge (Vector2 oldPos1,Vector2 oldPos2,Vector2 newPos1,Vector2 newPos2)
      float Pos_X1 = oldPos1.x - oldPos2.x;
      float Pos_Y1 = oldPos1.y - oldPos2.y;
      float length1 = Mathf.Sqrt (Pos_X1 * Pos_X1 + Pos_Y1 * Pos_Y1);

      float Pos_X2 = newPos1.x - newPos2.x;
      float Pos_Y2 = newPos1.y - newPos2.y;
      float length2 = Mathf.Sqrt (Pos_X2 * Pos_X2 + Pos_Y2 * Pos_Y2);

      if (length1 < length2)
          return true;//Enlarge gesture
          return false;//Gesture reduction

  void Start(){
      Input.multiTouchEnabled = true;//Turn on multi touch

  void Update(){
      if (Input.touchCount <= 0)
      if (Input.touchCount == 1)
      if (Input.touchCount > 1) {

          //Record the movement distance of two fingers per frame
          Vector2 deltaDis1 = new Vector2 ();
          Vector2 deltaDis2 = new Vector2 ();
          for (int i = 0; i < 2; i++) {
              Touch touch = Input.touches [i];
	      	  //Limit your touch,		
              if (touch.position.x>(0.33f*Screen.width)&&touch.position.x<(0.67*Screen.width)) 

                  if (touch.phase == TouchPhase.Ended) //Fingers out of touch
                  if (touch.phase == TouchPhase.Moved) { //Fingers in touch movement
                      if (i == 0) {			//Record the new position of the first finger
                          newPos1 = touch.position;
                          deltaDis1 = touch.deltaPosition;
                      } else {				//Record the new position of the second finger
                          newPos2 = touch.position;
                          deltaDis2 = touch.deltaPosition;

                          if (isEnlarge (oldPos1, oldPos2, newPos1, newPos2))
                          //Zoom in, zoom in
                              isforward = -1;
                              isforward = 1;
                      //Backup last touch position for comparison
                      oldPos1 = newPos1;
                      oldPos2 = newPos2;

                  //Zoom angle
				   //Calculate the distance between new and old touch points
                  float dis = Mathf.Abs(deltaDis1.x+deltaDis2.x)+Mathf.Abs(deltaDis1.																		 y+deltaDis2.y);
				   //Limit the degree of view zoom
                  if (Camera.main.fieldOfView >= 30 && Camera.main.fieldOfView <= 90)				   
                      //Control zoom of field of vision according to gesture
                      Camera.main.fieldOfView += isforward * (dis / 10);
                      if (Camera.main.fieldOfView < 30)
                          Camera.main.fieldOfView = 30;
                      if (Camera.main.fieldOfView > 90)
                          Camera.main.fieldOfView = 90;

Published 4 original articles, won praise 0, visited 18
Private letter follow

Posted by natbrazil on Thu, 16 Jan 2020 08:49:38 -0800