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
0
Question by andycodes · Sep 23, 2014 at 11:39 PM · guimobile

how to translate key input controls to touch controls? (For Mobile gaming)

ive been fiddling around for weeks now trying everything i can think of to make my controls work from keyboard input to gui input but i havent to get them to function right so im going to go back to square 1 and reach out and see if anyone can lead me in the right direction

i have these 2 scripts for controlling my player via keyboard input

this is for attacking an enemy

 using UnityEngine;
 using System.Collections;
 
 public class attacks : MonoBehaviour {
 
     public int speed = 2;
     private int attack = 0;
 
     Animator anim;
 
     void Start () 
     {
         anim = GetComponent<Animator>();
     }
     
 
     void Update () 
     {
         attack = 0;
         speed = 0;
 
         if(Input.GetKeyDown(KeyCode.Q))
           {
             attack = 1;
         }
         if(Input.GetKeyDown(KeyCode.W))
            {
             attack = 2;
         }
         if(Input.GetKeyDown(KeyCode.E))
         {
             attack = 3;
         }
         if(Input.GetKeyDown(KeyCode.R))
            {
             attack = 4;
         }
 
         if(Input.GetKey(KeyCode.S))
            {
             attack = -1;
 
         }
 
         anim.SetInteger("attack", attack); 
     }
 }


each "attack" value corresponds to an animation parameter to call an animation

this script is for moving left and right

 using UnityEngine;
 using System.Collections;
 
 public class playermovement : MonoBehaviour 
 {
     public int speed = 2;
     private int direction = 0;
     
     
     Animator anim;
     
     void Start ()
     {
         anim = GetComponent<Animator>();
     }
     
     void Update () 
     {
         direction = 0;
         
         if(Input.GetKey(KeyCode.D)) 
         {
 
             transform.Translate(Vector2.right * speed * Time.deltaTime);
 
             direction = 1;
         }
         
         
         if(Input.GetKey(KeyCode.A)) 
         {
 
             transform.Translate(-Vector2.right * speed * Time.deltaTime);
 
             direction = -1;
         }
 
         if(Input.GetKey(KeyCode.D) 
             && Input.GetKey(KeyCode.A))
            {
             direction = 0;
         }
         anim.SetInteger("speed", direction);
     }
     
     
 }


the "direction" values correspond to left or right direction in the animator

i don't want this done totally for me, i want to know how to do it and be able to recreate this in the future, but i would appreciate it if someone could give me a general idea of what functions i should be using

by the way, my GUI buttons were made using unity 4.6 with the canvas Button UI feature if that makes a difference on how it should be done

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
1

Answer by Freaking-Pingo · Sep 24, 2014 at 12:21 AM

Okay, I'll provide you with a directional hint then. In the new GUI system, you can see on the Button component, that they have a "On Click()" box at the bottom of the Button component. This box is capable of manipulating variables or even execute methods in the custom scripts you are making. However, your current code isn't really setup for this yet, because all your input logic is handled within the Update loop. Instead I'll suggest, for starters, you drop each logical input into each own separate method. So, for example:

 public void MoveRight()
 {
    //Add code, that enables moving right.
 }

If you know select your GUI button, which corresponds to "Going Right", you click the "+" sign in the OnClick box, drag the GameObject which contains the playermovement script into the object field, find the corresponding function you just created "playermovement > MoveRight", and wola, when hitting the button the MoveRight method should now be executed instead.

alt text alt text


screenshot 2014-09-24 02.10.15.png (27.1 kB)
screenshot 2014-09-24 02.10.39.png (11.6 kB)
Comment
Add comment · Show 3 · 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 andycodes · Sep 24, 2014 at 12:35 AM 0
Share

so will i need to make 7 separate scripts for each motion then?

avatar image Freaking-Pingo · Sep 24, 2014 at 12:44 AM 1
Share

Not seven seperate scripts, but seven different methods within the same script.

 public class playermovement : $$anonymous$$onoBehaviour 
 {
    public void $$anonymous$$oveRight()
    {
       // Add logic
    }
 
    public void $$anonymous$$oveLeft()
    {
       // Add logic
    }
 
    public void $$anonymous$$oveUp()
    {
       // Add logic
    }

    // And so forth...
 }

This solution, however, will properly not fulfill what you currently need, but it is a step in the direction. Each of these method calls are only executed once, as you push the designated GUI button, so you will somehow need to find an approach that allows you for registering either a continuos touch, or a On Touch / Leave Touch.

avatar image andycodes · Sep 24, 2014 at 12:54 AM 0
Share

alright, thanks for the input!

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

26 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Adding Touch Controls to Car Game 1 Answer

This line of code works in the editor, but not on my android device 1 Answer

destroy gui after game paused 3 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