Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to add touch buttons in a platform game?

Discussion in '2D' started by medgo99, Jan 17, 2020.

  1. medgo99

    medgo99

    Joined:
    Jan 6, 2020
    Posts:
    3
    I am developing a 2D video game for android, the only thing I need to finish is to make the touch controls. The video game only contains movement to the right and left and jump (like MarioBros). I have already created the buttons on canvas, I only need the script, I would greatly appreciate your help, I enclose my player code in case you can help me with the modifications.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7.  
    8. public class PlayerController : MonoBehaviour
    9. {
    10.     public float jumpPower = 15f;
    11.     private bool jump;
    12.     public bool grounded;
    13.     private Rigidbody2D rb2d;
    14.     private Animator anim;
    15.     private GameObject healthbar;
    16.     public GameObject panelGameOver;
    17.  
    18.  
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         rb2d = GetComponent<Rigidbody2D>();
    24.         anim = GetComponent<Animator>();
    25.  
    26.         healthbar = GameObject.Find("Healthbar");
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
    33.         {
    34.             jump = true;
    35.         }
    36.         anim.SetBool("Grounded", grounded);
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41.  
    42.         {
    43.             if (Input.GetKey(KeyCode.RightArrow))
    44.             {
    45.                 if (GetComponent<SpriteRenderer>().flipX == true)
    46.                 {
    47.                     GetComponent<SpriteRenderer>().flipX = false;
    48.                 }
    49.                 GetComponent<Animator>().SetBool("Correr", true);
    50.                 transform.Translate(0.1f, 0, 0);
    51.             }
    52.             if (Input.GetKey(KeyCode.LeftArrow))
    53.             {
    54.                 if (GetComponent<SpriteRenderer>().flipX == false)
    55.                 {
    56.                     GetComponent<SpriteRenderer>().flipX = true;
    57.                 }
    58.                 GetComponent<Animator>().SetBool("Correr", true);
    59.                 transform.Translate(-0.1f, 0, 0);
    60.             }
    61.             if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
    62.             {
    63.                 GetComponent<Animator>().SetBool("Correr", false);
    64.             }
    65.             //Saltar
    66.             if (jump)
    67.             {
    68.                 rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
    69.                 jump = false;
    70.             }
    71.  
    72.         }
    73.  
    74.  
    75.  
    76.  
    77.  
    78.  
    79.         //
    80.     }
    81.     public void EnemyJump()
    82.     {
    83.         jump = true;
    84.  
    85.  
    86.     }
    87.  
    88.     public void EnemyJumpB()
    89.     {
    90.         healthbar.SendMessage("TakeDamage", 30);
    91.         jump = true;
    92.  
    93.     }
    94.  
    95. }
     
  2. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    It is very easy.
    In the script you need to make public void
    and write what do you want to do.
    In the settings of buttons at the bottom you will see" runtime only " and below it will be empty filed drag a prefab that contains script you want to do and on right choose script and void that contains action, for example
    Code (CSharp):
    1.  
    2. public void jump(){
    3. if(grounded)
    4.         {
    5.             jump = true;
    6.         }
    7. }
    You can don't edit your script just add public voids
     
  3. medgo99

    medgo99

    Joined:
    Jan 6, 2020
    Posts:
    3
    Thank's! i try this but not work in my android device :(((( (sorry i am from mexico and no speak english)
     
  4. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    try to use yandex translator