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
1
Question by Albert-han · Jun 10, 2014 at 08:56 AM · guitexturecontrollertouchscreen

Touchscreen for unity

Hi guys i already finished my game but now i need to know how to make it touchscreen.I am using a character controller that only uses right,left and jump (2d game) and want to make it touchscreen but i have really no idea where to start to make it touchscreen.Do i just have to make gui textures and add a script to it? Can anybody give me some clues?thanks

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Jun 10, 2014 at 10:31 AM 0
Share

Not sure that you finished your game if you still need to add touch controls. ;-)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dbrizov · Jun 10, 2014 at 10:12 AM

One way to do this is to create 3DText GameObjects for the left, right and jump key. You will then need to give them BoxCollider components. Once this is done you need to Raycast a ray to the touch position and check if you've hit one of the 3DText's colliders. If you hit the left 3DText, then you simply move the character to the left and so on.

Have a look at this pseudo-code. I Hope it helps:

 public class PlayerController : MonoBevaviour
 {
     // Those 3 are the 3DTexts in the scene
     private GameObject leftArrow;
     private GameObject rightArrow;
     private GameObject jumpKey;
 
     private void Start()
     {
         // Find the 3DTexts in the scene
         // Another way will be to make the game objects public and drag and drop them from the editor
         this.leftArrow = GameObject.Find("Left Arrow"); // The name of the leftArrow 3DText is "Left Arrow"
         this.rightArrow = GameObject.Find("Right Arrow");
         this.jumpKey = GameObject.Find("Jump Key");
     }
 
     private void Update()
     {
         // If we have touched the screen
         if (Input.touchCount > 0)
         {
             foreach (var touch in Input.touches)
             {
                 // If the touch began, or moved, or is on the screen but stationary
                 if (touch.phase == TouchPhase.Began ||
                     touch.phase == TouchPhase.Moved ||
                     touch.phase == TouchPhase.Stationary)
                 {
                     Ray ray = Camera.main.ScreenPointToRay(touch.position);
                     RaycastHit hit;
 
                     float rayLength = 100.0f;
 
                     if (Physics.Raycast(ray, out hit, rayLength))
                     {
                         // If the ray hit the left arrow game object
                         if (hit.collider.gameObject == this.leftArrow)
                         {
                             // Move the player to the left
                         }
                         else if (hit.collider.gameObject == this.rightArrow)
                         {
                             // Move the player to the right
                         }
                         else if (hit.collider.gameObject == this.jumpKey)
                         {
                             // Make the player to jump
                         }
                     }
                 }
             }
         }
     }
 }
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
avatar image
1

Answer by screenname_taken · Jun 10, 2014 at 10:44 AM

I'm a bit against using 3D text when possible as you get lots of extra draw calls. It really just needs a guitexture with a touch script. Or if you need a mesh, just a plane mesh with a script to always look towards the camera and a collider for ray casting.

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

24 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

Related Questions

Help with destroying guiRect? 0 Answers

Make more buttons appear, on button click. 1 Answer

scaling behaviour of the standard box texture? 0 Answers

Blurry Texture Help 5 Answers

reference Gui texture outside script 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