Search Unity

2D touchScreen buttons wont work.

Discussion in '2D' started by s4ddo, Dec 31, 2017.

  1. s4ddo

    s4ddo

    Joined:
    Dec 31, 2017
    Posts:
    1
    So I have been trying to code a game with touch functions, and i have been going through hourse of research and trial and error but it doesnt seem to ever work. I made 2 scripts first of which was the CharacterControls
    and then TouchControls,

    Character Controls Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterControls : MonoBehaviour {
    6.     public GameObject playerBullet;
    7.     public GameObject FirePoint;
    8.     public float moveSpeed;
    9.     public float jumpHeight;
    10.     private float moveVelocity;
    11.  
    12.     public Transform groundCheck;
    13.     public float groundCheckRadius;
    14.     public LayerMask whatIsGround;
    15.     private bool grounded;
    16.     private Animator anim;
    17.     private float playersx;
    18.     // Use this for initialization
    19.     void Start () {
    20.         anim = GetComponent<Animator> ();
    21.         playersx = transform.localScale.x;
    22.     }
    23.  
    24.     void FixedUpdate(){
    25.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
    26.     }
    27.  
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.  
    32.         anim.SetBool ("Grounded", grounded);
    33.         #if UNITY_STANDALONE || UNITY_WEBPLAYER
    34.         Move (Input.GetAxisRaw ("Horizontal"));
    35.  
    36.  
    37.  
    38.         if (Input.GetButtonDown ("Fire1")) {
    39.             GameObject bullet01 = (GameObject)Instantiate (playerBullet);
    40.             bullet01.transform.position = FirePoint.transform.position;
    41.         }
    42.  
    43.  
    44.  
    45.         if(Input.GetKeyDown (KeyCode.Space) && grounded)
    46.             {          
    47.             GetComponent<Rigidbody2D> ().velocity = new Vector2(GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);
    48.             }
    49.         if(Input.GetKey (KeyCode.D))
    50.         {          
    51.             GetComponent<Rigidbody2D> ().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
    52.         }
    53.         if(Input.GetKey (KeyCode.A))
    54.         {          
    55.             GetComponent<Rigidbody2D> ().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
    56.         } #endif
    57.  
    58.         anim.SetFloat ("Speed", Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x));
    59.         if (GetComponent<Rigidbody2D> ().velocity.x > 0)
    60.             transform.localScale = new Vector3 (playersx, transform.localScale.y, transform.localScale.z);
    61.         else if (GetComponent<Rigidbody2D> ().velocity.x < 0)
    62.             transform.localScale = new Vector3 (-playersx, transform.localScale.y, transform.localScale.z);
    63.         }
    64.  
    65.  
    66.     public void Move(float moveInput)
    67.     {
    68.         moveVelocity = moveSpeed * moveInput;
    69.     }
    70. }
    TouchControls Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class TouchControls : MonoBehaviour {
    6.  
    7.     private CharacterControls thePlayer;
    8.  
    9.     void Start(){
    10.         thePlayer = FindObjectOfType<CharacterControls> ();
    11.     }
    12.     public void LeftArrow()
    13.     {
    14.         thePlayer.Move (-1);
    15.     }
    16.     public void RightArrow()
    17.     {
    18.         thePlayer.Move (1);
    19.     }
    20.     public void Stop(){
    21.         thePlayer.Move (0);
    22.     }
    23. }
    Please Help, Thanks to anyone who is able to help me out.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,443
    Does the keyboard version work?

    If you put Debug.Log() inside those Left/Right arrow functions, does it get called?

    One difference there could be that this line gets called inside update loop,
    Move (Input.GetAxisRaw ("Horizontal"));

    but maybe these line gets only called once per button click? (from where and how you are calling these left right calls?)
    public void LeftArrow()