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 matta9001 · Mar 12, 2014 at 03:53 AM · javascriptmobileiphonenoob

Transfer controls from keyboard buttons to IPhone buttons

Luckily my game only has two buttons. Haha so i wanted to make a simple IPhone game but since i am not really used to iphone inputs i just wanted to test it with the keyboard. So all my game is, is going left and right. But i want it so that if you touch the right side of the screen, the ship goes to the right, and if you touch the left side of the screen, it goes left. So this is my script #pragma strict

 var isIdle = true;
 var isLeft = false;
 var isRight = false;
 
 function Update () {
     var translation : float = Time.deltaTime * 3;
     gameObject.animation.wrapMode = WrapMode.Loop;
     
     if (Input.GetKeyDown(KeyCode.A)){
     isIdle = false;
     isLeft = true;
     isRight = false;
     }
     if (Input.GetKeyDown(KeyCode.D)){
     isIdle = false;
     isLeft = false;
     isRight = true;
     }
     if (Input.GetKeyUp(KeyCode.A)){
     isIdle = true;
     isLeft = false;
     isRight = false;
     }
     if (Input.GetKeyUp(KeyCode.D)){
     isIdle = true;
     isLeft = false;
     isRight = false;
     }
     if (isIdle == true) {
         gameObject.animation.Play("Idle");
     }
     if (isLeft == true){
         gameObject.animation.Play("MoveLeft");
         transform.Translate (-translation, 0, 0,Space.World);
     }
     if (isRight == true){
         gameObject.animation.Play("MoveRight");
         transform.Translate (translation, 0, 0, Space.World);
     }
     
 }

how do i convert it from "A" and "D", to these new Iphone inputs?

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
Best Answer

Answer by fifthknotch · Mar 12, 2014 at 04:31 AM

Here is a nice trick and is the quickest way to get you heading in the right direction. OnMouseDown functions work with mobile input. Pretty much, you have to triggers (one for right and one for left) that cover your cameras view and are see-through. You will need to separate your code into two separate scripts, one to handle right movement and the other to handle left movement. Now, hear is the downside to using OnMouseDown: Their is only ONE mouse. If you touch the screen with two fingers, the touch screen reads the input as a mouse click at the midpoint of the two touches. So, in effect, OnMouseDown is not multitouch supportive, but it is the easiest way of converting your scripts. That is one way of doing it however.

The best way to accomplish transitioning between pc to mobile input is to make two invisible GUITextures (similar to the above method's triggers). Using these GUITextures, you will check for a hitTest on that texture. Then, move left or right accordingly. The only difference between this method and the one above is this method is multitouch supportive. This is because instead of using an invisible mouse as your touch input, it actually uses your fingers. Here is a quiche snippet of code over how to check for hitTest:

 var moveLeft : GUITexture;
 var moveRight : GUITexture;
 
 function Update () {
       for (var touch : Touch in Input.touches) {
             if (moveLeft.HitTest( touch.position )) {
                   rigidbody.AddForce(2,0,0);
             }
             if (moveRight.HitTest( touch.position )) {
                   rigidbody.AddForce(-2,0,0);
             }
       }
 }

*Note: If you use the above code, it does not support computer input. You must either test using the Unity Remote or build the game to your device.

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 matta9001 · Mar 22, 2014 at 05:43 PM 0
Share

Sorry for the way late response but if the texture is invisible how do i know the boundaries?

avatar image matta9001 · Mar 22, 2014 at 05:49 PM 0
Share

I need just a bit of help actually getting the GUI there, notice the noob tag

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

21 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

Related Questions

Really simple IPhone and screen question 0 Answers

Texture as a simple button 1 Answer

Optimal # of Faces for many mesh scene for mobile 1 Answer

Call previous level, as for whatever it was 0 Answers

flying should only be possible while fuel 0 < but it continues so long as space is held 2 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