Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Help with Mobile Touch Programming

Discussion in 'Scripting' started by hembrid0123, Sep 1, 2020.

  1. hembrid0123

    hembrid0123

    Joined:
    Sep 1, 2020
    Posts:
    5
    This is my First Thread so i dont know i covered everything needed.
    -----------------------------------------------------------------------------------------
    Hello i need help to assign Touch Controls to my Script.

    I used this Tutorial:
    .

    Thats my Code so far:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class GameController : MonoBehaviour
    9. {
    10.     public int maxSize;
    11.     public int currentSize;
    12.     public int xBound;
    13.     public int yBound;
    14.     public int score;
    15.     public GameObject foodPrefab;
    16.     public GameObject currentFood;
    17.     public GameObject snakePrefab;
    18.     public GameObject deathMenuUI;
    19.     public Snake head;
    20.     public Snake tail;
    21.     public int NESW;
    22.     public Vector2 nextPos;
    23.     public Vector2 startPos;
    24.     public Vector2 endPos;
    25.     public Text scoreText;
    26.     public Text scoreTextD;
    27.     bool directionChosen;
    28.  
    29.     void OnEnable()
    30.     {
    31.         Snake.hit += hit;
    32.     }
    33.  
    34.     void Start()
    35.     {
    36.         InvokeRepeating("TimerInvoke", 0, .3f);
    37.         FoodFunction();
    38.     }
    39.  
    40.     void OnDisable()
    41.     {
    42.         Snake.hit -= hit;
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         ComChangeD();
    48.  
    49.         if (Input.touchCount > 0)
    50.         {
    51.             Touch touch = Input.GetTouch(0);
    52.  
    53.             switch (touch.phase)
    54.             {
    55.                 case TouchPhase.Began:
    56.                     startPos = touch.position;
    57.                     directionChosen = false;
    58.                     break;
    59.  
    60.                 case TouchPhase.Ended:
    61.                     endPos = touch.position;
    62.                     directionChosen = true;
    63.                     break;
    64.             }
    65.         }
    66.     }
    67.  
    68.     void FixedUpdate()
    69.     {
    70.         if (directionChosen)
    71.         {
    72.             if (startPos.x < endPos.x)
    73.             {
    74.                 //
    75.             }
    76.             else if (startPos.x > endPos.x)
    77.             {
    78.                 //
    79.             }
    80.  
    81.             directionChosen = false;
    82.        
    83.         }
    84.     }
    85.  
    86.     void TimerInvoke()
    87.     {
    88.         Movement();
    89.         if(currentSize >= maxSize)
    90.         {
    91.             TailFunction();
    92.         }
    93.         else
    94.         {
    95.             currentSize++;
    96.         }
    97.     }
    98.  
    99.     void Movement()
    100.     {
    101.         GameObject temp;
    102.         nextPos = head.transform.position;
    103.         switch(NESW)
    104.         {
    105.             case 0:
    106.                 nextPos = new Vector2(nextPos.x, nextPos.y + 1);
    107.                 break;
    108.             case 1:
    109.                 nextPos = new Vector2(nextPos.x + 1, nextPos.y);
    110.                 break;
    111.             case 2:
    112.                 nextPos = new Vector2(nextPos.x, nextPos.y - 1);
    113.                 break;
    114.             case 3:
    115.                 nextPos = new Vector2(nextPos.x - 1, nextPos.y);
    116.                 break;
    117.         }
    118.  
    119.         temp = (GameObject)Instantiate(snakePrefab, nextPos, transform.rotation);
    120.         head.SetNext(temp.GetComponent<Snake>());
    121.         head = temp.GetComponent<Snake>();
    122.  
    123.         return;
    124.     }
    125.  
    126.     void ComChangeD()
    127.     {
    128.         if (NESW != 2 && Input.GetKeyDown(KeyCode.W))
    129.         {
    130.             NESW = 0;
    131.         }
    132.         if (NESW != 3 && Input.GetKeyDown(KeyCode.D))
    133.         {
    134.             NESW = 1;
    135.         }
    136.         if (NESW != 0 && Input.GetKeyDown(KeyCode.S))
    137.         {
    138.             NESW = 2;
    139.         }
    140.         if (NESW != 1 && Input.GetKeyDown(KeyCode.A))
    141.         {
    142.             NESW = 3;
    143.         }
    144.     }
    145.  
    146.     void TailFunction()
    147.     {
    148.         Snake tempSnake = tail;
    149.         tail = tail.GetNext();
    150.         tempSnake.RemoveTail();
    151.     }
    152.  
    153.     void FoodFunction()
    154.     {
    155.         int xPos = Random.Range(-xBound, xBound);
    156.         int yPos = Random.Range(-yBound, yBound);
    157.  
    158.         currentFood = (GameObject)Instantiate(foodPrefab, new Vector2(xPos, yPos), transform.rotation);
    159.         StartCoroutine(CheckRender(currentFood));
    160.     }
    161.  
    162.     IEnumerator CheckRender(GameObject IN)
    163.     {
    164.         yield return new WaitForEndOfFrame();
    165.         if(IN.GetComponent<Renderer>().isVisible == false)
    166.         {
    167.             if(IN.tag == "Food")
    168.             {
    169.                 Destroy(IN);
    170.                 FoodFunction();
    171.             }
    172.         }
    173.     }
    174.  
    175.     void hit(string WhatWasSent)
    176.     {
    177.         if(WhatWasSent == "Food")
    178.         {
    179.             FoodFunction();
    180.             maxSize++;
    181.             score++;
    182.             scoreText.text = score.ToString();
    183.             scoreTextD.text = score.ToString();
    184.             int temp = PlayerPrefs.GetInt("HighScore");
    185.             if(score > temp)
    186.             {
    187.                 PlayerPrefs.SetInt("HighScore", score);
    188.             }
    189.         }
    190.  
    191.         if(WhatWasSent == "Snake")
    192.         {
    193.             CancelInvoke("TimerInvoke");
    194.             deathMenuUI.SetActive(true);
    195.         }
    196.  
    197.         if(WhatWasSent == "Wall")
    198.         {
    199.             CancelInvoke("TimerInvoke");
    200.             deathMenuUI.SetActive(true);
    201.         }
    202.     }
    203.  
    204.     public void Menu()
    205.     {
    206.         SceneManager.LoadScene(0);
    207.     }
    208.  
    209.     public void ExitGame()
    210.     {
    211.         Application.Quit();
    212.         Debug.Log("QUIT");
    213.     }
    214. }
    215.  
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. public class Snake : MonoBehaviour
    8. {
    9.     private Snake next;
    10.     static public Action<String> hit;
    11.  
    12.     public void SetNext(Snake IN)
    13.     {
    14.         next = IN;
    15.     }
    16.  
    17.     public Snake GetNext()
    18.     {
    19.         return next;
    20.     }
    21.  
    22.     public void RemoveTail()
    23.     {
    24.         Destroy(this.gameObject);
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider other)
    28.     {
    29.         if(hit != null)
    30.         {
    31.             hit(other.tag);
    32.         }
    33.  
    34.         if(other.tag == "Food")
    35.         {
    36.             Destroy(other.gameObject);
    37.         }
    38.     }
    39. }
    40.  
    There are other Scripts to but i dont think that this Scrips are needed for a solution
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    The code you shared seems to have some touch support in it already. Are you running into a specific issue or error that you can't resolve?
     
  3. hembrid0123

    hembrid0123

    Joined:
    Sep 1, 2020
    Posts:
    5
    Sorry that i didnt reply earlier.
    I need to attach my Movement to the Touch Controls and i don´t know how.