- Home /
Button Touch for Android
Hi.
I can't seem to figure out how to create a button for an android device. What I mostly want right now is if the button is pressed and held, the variable will be true. (example: if the shoot button is held, the spaceship continues to shoot.) I can make the spaceship shoot, but input.touch seems to elude my understanding!
I'd prefer the answer in C#, but I can work with java, and would appreciate any feedback!
Thanks!
Answer by Sandy Sandals · Apr 14, 2014 at 07:17 AM
Hey there! So I would highly recommend you follow this video tutorial. It is very good and will help a lot. https://www.youtube.com/watch?v=SrCUO46jcxk (It works on both Android and iOS)
On my Android game I then made a few panels (they were invisible) in the scene that could be viewed by the camera (it was 2D). Then I added scripts to these panels that looked kind of like this;
 public PlayerController playerController;
 
 void OnTouchDown() {
     playerController.setLeftDown(true); // Obviosuly this can be extended to more than left and right booleans
 }
I hope that this helps and if you have any questions feel free to ask!! :)
I know this thread is old, but if you can find it, could you send the final script that you used for your player controller for mobile?
Answer by rohitpython12 · Jul 11, 2020 at 12:13 PM
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Character : MonoBehaviour {
 [HideInInspector] public bool facingRight = true;
 [HideInInspector] public float moveForce = 50f;
 [HideInInspector] public float maxSpeed = 10f;
 [HideInInspector] public float currentSpeed = 0.0f;
 [HideInInspector] public float touchRun = 0.0f;
 [HideInInspector] public float jumpSpeed = 0.0f;
 private Rigidbody2D rb2d;
 private float ScreenWidth;
 private Animator animator;
 public Transform groundCheck;
 public bool grounded = false;
 public float jumpForce = 800f;
 private bool jump = false;
 // Use this for initialization
 void Awake () {
     rb2d = GetComponent();
     ScreenWidth = Screen.width;
     animator = GetComponent ();
 }
 // Update is called once per frame
 void Update () {
     grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
     animator.SetBool ("isGrounded", grounded);
     if (Input.GetButtonDown ("Jump")) {
         jump = true;
     }
     
     touchRun = 0.0f;
     //-----TOUCH CONTROLS------
     int i = 0;
     //loop over every touch found
     while (i < Input.touchCount) {
         if (Input.GetTouch (i).position.x < ScreenWidth / 4) {
             //move left
             touchRun = -1.0f;
         } else if (Input.GetTouch (i).position.x < ScreenWidth / 2) {
             //move right
             touchRun = 1.0f;
         }
         if (Input.GetTouch (i).phase == TouchPhase.Began && Input.GetTouch (i).position.x > ScreenWidth * 0.5) {
             jump = true;
         }
         ++i;
     }
     //-----CONTROLLER------
     #if UNITY_EDITOR
     touchRun = Input.GetAxis("Horizontal");
     #endif
 }
 void FixedUpdate(){
     moveCharacter(touchRun);
     if (jump)
         JumpCharacter ();
     float characterSpeed = Mathf.Abs (rb2d.velocity.x);
     animator.SetFloat ("Speed", characterSpeed);
 }
 void JumpCharacter(){
     if (grounded) {
         rb2d.AddForce (new Vector2 (0f, jumpForce));
         grounded = false;
     }
     jump = false;
 }
 void moveCharacter(float h){
     //float modMoveForce = grounded ? moveForce : moveForce * 1; //slow down if in air
     if (h * rb2d.velocity.x < maxSpeed)
         rb2d.AddForce (Vector2.right * h * moveForce);
     if (Mathf.Abs (rb2d.velocity.x) > maxSpeed)
         rb2d.velocity = new Vector2 (Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
     if (h > 0 && !facingRight)
         Flip ();
     else if (h < 0 && facingRight)
         Flip ();
 }
 void Flip()    {
     facingRight = !facingRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
     rb2d.velocity = new Vector2 (rb2d.velocity.x * 0.75f, rb2d.velocity.y);
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                