Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by SuperChris01 · Apr 13, 2014 at 08:12 PM · androidbuttontouchgui-buttoninput.touch

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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!! :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperChris01 · Apr 18, 2014 at 07:34 PM 0
Share

Thanks. I'll look into it!

avatar image rory_chang05 · Aug 02, 2020 at 02:17 PM 0
Share

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?

avatar image
0

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);

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

touch GUITexture Help 1 Answer

Touches aren't working on Android 1 Answer

Android Buttons Responding Poorly To Touch Controls 2 Answers

How can I get Rect.Contains to work properly with touch input? 1 Answer

Button Focus Prevents Touch Raycast (Android C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges