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 Reaksmey-Rt · Jan 19, 2015 at 01:15 PM · touch screen

TouchScreen

Dear Sir/Madam i got a problem with "how to control my game to move up,down,left and right" .The first I control my moving game with button key(left,right,up and down).But I want to control my moving game with touch on screen.so how can i solve it? please see my code to control on Key Button. +++++++++++++++++++++++++++++++Code+++++++++++++++++++++

public void playerMove() {

     GameObject Moverment = GameObject.Find("Player");
     float moveHorizontal = Input.GetAxis("Horizontal");

     float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    Moverment.rigidbody.velocity = movement * SingletonManage.Instance.gameData_Ref.speed;
    Moverment.rigidbody.position = new Vector3
   (
       Mathf.Clamp(Moverment.rigidbody.position.x, SingletonManage.Instance.gameData_Ref.xMin, SingletonManage.Instance.gameData_Ref.xMax),
       7f,
       Mathf.Clamp(Moverment.rigidbody.position.z, SingletonManage.Instance.gameData_Ref.zMin, SingletonManage.Instance.gameData_Ref.zMax)
   );

     Moverment.rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, Moverment.rigidbody.velocity.x *  -(SingletonManage.Instance.gameData_Ref.tilt));
 }


   
Comment
Add comment · Show 2
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 SaraCecilia · Jan 19, 2015 at 01:55 PM 1
Share

Hey, you can find examples in our documentation for mobile device input here: http://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$obileInput.html

Simple example here: http://docs.unity3d.com/ScriptReference/Input-touches.html

avatar image Zennig · Jan 19, 2015 at 07:57 PM 1
Share

There is a recent live session video, showing how the process to simple create a touch control. (using the space shooter scenes). The video is hosted by $$anonymous$$Buckner.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/space-shooter-to-mobile

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by zharik86 · Jan 19, 2015 at 07:49 PM

Simple way: create GUITexture as joystick for touch. Than (write on CSharp):

  public GUITexture myMoveBtn = null; //change, reference on your gui texture
  private int finId1 = -1; //id finger for cancel touch event

  private GameObject Moverment = null;
  //UPDATE 1: use global variable for your player object
  void Start() {
   //Only one time find your game object
   Moverment = GameObject.Find("Player");
  }

  void Update() {
   //Check count of touches
   if(Input.touchCount > 0) {
    foreach (Touch touch in Input.touches) { 
     if (touch.phase  == TouchPhase.Began && myMoveBtn.HitTest(touch.position) && finId1 == -1) {
      finId1 = touch.fingerId; //store Id finger
      //Do stuff
     }
     //Move
     if(touch.fingerId == finId1) {
      //Calc right angle
      //UPDATE 1: using center of GUITexture
      float ang = Mathf.Atan2(touch.position.x - myMoveBtn.GetScreenRect().center.x, touch.position.y - myMoveBtn.GetScreenRect().center.y)
      //your code
      float moveHorizontal = 1.0f * Mathf.Sin(ang);
      float moveVertical = 1.0f * Mathf.Cos(ang);
      //An etc your code, where you calculate move
      //...
     }
     if (touch.phase == TouchPhase.Ended) { //correct end of touches
      if(touch.fingerId == finId1) { //check id finger for end touch
       finId1 = -1;
       //UPDATE 1: stop rigidbody, when you release GUITexture
       Moverment.rigidbody.velocity = new Vector3(0, 0, 0); //you can use "Vector3.zero" too.
       Moverment.rigidbody.angularVelocity = Vector3.zero;
      }
     }
    }
   }
  }

I hope that it will help you.

P.S.: Realy touch checks only real device or Eclipse emulator of android (AVD).

Comment
Add comment · Show 8 · 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 Reaksmey-Rt · Jan 20, 2015 at 07:21 AM 0
Share

Thank you! For answer this problem and i also try to follow you but something have err.. So can you detail it for me ? my$$anonymous$$oveBtn.HitTest(touch.position)

my$$anonymous$$oveBtn.GetScreeRect().x

avatar image zharik86 · Jan 20, 2015 at 07:53 AM 1
Share

Oh, sorry, "my$$anonymous$$oveBtn" must have another type: GUITexture. I change it in my answer.

avatar image Reaksmey-Rt · Jan 20, 2015 at 09:06 AM 0
Share

Oh.. Thank you and try to follow that and have 1 more err..

my$$anonymous$$oveBtn.GetScreeRect().x, my$$anonymous$$oveBtn.GetScreeRect().y

avatar image Reaksmey-Rt · Jan 20, 2015 at 09:06 AM 0
Share

GetScreeRect()

avatar image zharik86 · Jan 20, 2015 at 09:16 AM 1
Share

Ok, ins$$anonymous$$d "GetScreeRect", write "GetScreenRect". I update this. Hope, no error:)

Show more comments
avatar image
1

Answer by MarlonH · Jan 20, 2015 at 11:37 AM

Hello! You can use "Imput.TouchCont" and "Imput.GetTouch (int Touch)" to work with touchs. Within the GetTouch, you can take the position of the touch (Imput.GetTouch (0) .position.x, for example). :)

Comment
Add comment · Show 1 · 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 Reaksmey-Rt · Jan 20, 2015 at 11:59 AM 0
Share

thank you

avatar image
1

Answer by Ali_unity · Jul 23, 2015 at 12:31 AM

The easiest way to implement touch controls for newbies like us is the asset "easy touch controls" in the asset store for 10$. I am new to unity and programming and this asset worked for me without any problem. that's very easy.

https://www.assetstore.unity3d.com/en/#!/content/28118

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

29 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

Related Questions

how to identify physical objects on a touch screen. 4 Answers

TouchScreenKeyboard.isSupported==false 1 Answer

Touch input not working 3 Answers

Get position of touch 2 Answers

Scripting for UI Buttons/Images 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