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 /
This question was closed Mar 11, 2015 at 01:57 PM by Sindorej for the following reason:

Outdated

avatar image
0
Question by Sindorej · Jan 25, 2014 at 11:42 AM · mobiletouchcontrols

Touch controls

I want to have a button to move my character for touch controls.

Like that on the bottom left corner of the image alt text

I have the following code but it does not move the button at all. It works ok when i use mouse input instead of touch. What do i do wrong?

 using UnityEngine;
 using System.Collections;
 
 public class mobile_control : MonoBehaviour {
     private int initx = 100;
     private int inity = Screen.height-70; 
     Rect buttonRect;
     bool buttonPressed = false;
     private int MaxClampX;
     private int MinClampX;
     private int MaxClampY;
     private int MinClampY;
     void Start(){
         buttonRect = new Rect (initx,inity, 40, 40);
         MaxClampX = initx + 80;
         MinClampX = initx - 60;
         MinClampY = inity;
         MaxClampY = inity - 60;
     }
     void OnGUI()
     {
         if(Input.touchCount > 0){
             if(Input.GetTouch(0).position.x<200 && Input.GetTouch(0).position.y > Screen.height - 200)
             {
                 buttonPressed = true;
             } else {
                 buttonPressed = false;
                 if(buttonRect.y<inity) buttonRect.y +=2;
                 if(buttonRect.y>=inity) buttonRect.y = inity;
                 if(buttonRect.x>initx+6) buttonRect.x -=2;
                 if(buttonRect.x<initx-6) buttonRect.x +=2;
             }
             
             if(buttonPressed && Input.GetTouch(0).phase == TouchPhase.Moved)
             {
                 Vector2 tdelta = Input.GetTouch(0).deltaPosition;
                 if(!(buttonRect.x+tdelta.x>=MaxClampX || buttonRect.x+tdelta.x<=MinClampX))
                     buttonRect.x += tdelta.x;
                 if(!(buttonRect.y+tdelta.y<=MaxClampY || buttonRect.y+tdelta.y>=MinClampY))
                     buttonRect.y += tdelta.y;
                 if(buttonRect.x>initx+10){
                     Debug.Log("Forward");
                     if(buttonRect.x>initx+60){
                         Debug.Log("Run");
                     }
                 }
                 if(buttonRect.x<initx-10){
                     Debug.Log("Backward");
                 }
                 if(buttonRect.y<inity-30){
                     Debug.Log("Jump");
                 }
             }
         } else {
             if(buttonRect.y<inity) buttonRect.y +=2;
             if(buttonRect.y>=inity) buttonRect.y = inity;
             if(buttonRect.x>initx+6) buttonRect.x -=2;
             if(buttonRect.x<initx-6) buttonRect.x +=2;
         }
         GUI.Button(buttonRect, "X");
     }
 }
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

18 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

Related Questions

when i drag my finger on mobile screen gameobject move left or right but not follow my finger 1 Answer

Deactivate Swipe Controls on Pause? 0 Answers

Multi touches problem 0 Answers

Cross platform input manager should return integers 0 Answers

Using Input.GetAxis variable for Touch Controls 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