Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by zann333 · Apr 29, 2016 at 11:36 AM · androidguiinputbuttons

How to replace keyboard inputs with touch inputs.

Hey guys, I am more of a designer not a programmer so i tried out this video tutorial on how to make a 2D fighting game. According to the inputs manager the events on the x, y axis are controlled with specific keys. But i want this game to be playable on android devices so i need UI buttons i suppose ? How can i kind of make a function that uses the already working code for PlayerControl and call them on button tap ? Or maybe there is another solution ? I need 5 buttons: left, right, jump, left punch, right punch. My number one target is to follow the line of this code because have few to zero skills in C#. I am using Unity 5.3.4f1, btw i think there might be some problems with UI buttons, not very sure.This is the code i currently use. Any help is very appreciated ! Thanks

using UnityEngine; using System.Collections;

public class PlayerControl : MonoBehaviour {

 public int PlayerNumber = 1;
 Transform enemy;

 Rigidbody2D rig2d;
 Animator anim;

 float horizontal;
 float vertical;
 public float maxSpeed = 25;
 Vector3 movement;
 bool crouch;

 public float Jumpforce = 20;
 public float jumpDuration = .1f;
 float jmpDuration;
 float jmpForce;
 bool jumpKey;
 bool falling;
 bool onGround;
   

 void Start()
 {
     rig2d = GetComponent<Rigidbody2D>();
     anim = GetComponentInChildren<Animator>();

 }

 void FixedUpdate()
 {
     //#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITYWINRT
     horizontal = Input.GetAxis("Horizontal" + PlayerNumber.ToString());
     vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());
     //#endif

    Vector3 movement = new Vector3(horizontal, 0, 0);
     crouch = (vertical < -0.1f);
     if (vertical > 0.1f)
     {
         if (!jumpKey)
         {
             jmpDuration += Time.deltaTime;
             jmpForce += Time.deltaTime;

             if (jmpDuration < jumpDuration)
             {
                 rig2d.velocity = new Vector2(rig2d.velocity.x, jmpForce);

             }
             else
             {
                 jumpKey = true;
             }
         }
     }

     if (!onGround && vertical < 0.1f)
     {
         falling = true;
     }
     if (!crouch)
         rig2d.AddForce(movement * maxSpeed);
     else
         rig2d.velocity = Vector3.zero;
 }

  void OnGroundCheck()
 {
     if (!onGround)
     {
         rig2d.gravityScale = 5;
     }

     else

     {
         rig2d.gravityScale = 1;
     }
 }


 void Update()
 {
     UpdateAnimator();
     OnGroundCheck();
 }

 void UpdateAnimator()
 {
     anim.SetBool("OnGround", this.onGround);
     anim.SetBool("Falling", this.falling);
     anim.SetBool("Crouch", crouch);
     anim.SetFloat("Movement", Mathf.Abs(horizontal));
 }
 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.collider.tag == "Ground")
     {
         onGround = true;

         jumpKey = false;
         jmpDuration = 0;
         jmpForce = Jumpforce;
         falling = false;
     }
 }
 void OnCollisionExit2D(Collision2D col)
 {
     if (col.collider.tag == "Ground")
         onGround = false;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jason2014 · May 02, 2016 at 02:45 PM

You have to write additional code for "Touch Input". See there: Adding Mobile Controls in Roguelike project

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

97 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 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 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 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 avatar image

Related Questions

How use ANDROID NAVIGATION BUTTONS 0 Answers

The UI Button Hitbox changes after using a Input Field on Android 1 Answer

Android C# touch buttons to change state 0 Answers

How to make Canvas appear after GUI texture? Unity 4.7 1 Answer

Input falsely recognized on a Samsung Galaxy Tab A7 0 Answers


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