• 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
Question by Peterw301 · Nov 02, 2012 at 10:23 AM · androidjavascriptguitouch

Android touch gui help!!!

I really want to make it so when you touch an npc it displays some gui, like a text box. Then next to it a few buttons and when you click the buttons its shows another text box and so on its for an android RPG I'm making and I'm really stuck with getting it so when you touch a npc it shows gui. im a massive newbie to scripting so please explain in detail. i know its a lot to ask but PLEASE HELP, thanks

 #pragma strict
 
 var show :  boolean;
 var show2 : boolean;
 var target : Transform;
 
    var screenPosX : Vector3 = camera.WorldToScreenPoint (target.position);
     print ("target is " + screenPosX.x + " pixels from the left");      
    var screenPosY : Vector3 = camera.WorldToScreenPoint (target.position);
     print ("target is " + screenPosY.y + " pixels from the top");
     
   // screen position of gameobject  
    
     var oSPX = Mathf.Round(screenPosX.x / 100);
     var oSPY = Mathf.Round(screenPosY.y / 100);
 if(show2 === true);
 {
   
 }
 function Update () {
  
  for (var touch : Touch in Input.touches) {
  if (touch.phase == TouchPhase.Began) {
  show = true;
  Debug.Log("niceballsTrue");
  Debug.Log(show);
     
     var touchPositionXRounded = Mathf.Round(touch.position.x / 100);
     var touchPositionYRounded = Mathf.Round(touch.position.y / 100);
 }
 else
 {
  show = false;
   Debug.Log("False");
   Debug.Log(show);
 }
 
 
 
 
 if(show === true)
 {
    Debug.Log("OMG IT WORKSish");
 }
 else if (touchPositionXRounded == oSPX) 
 {
    if (touchPositionYRounded ==  oSPY) {
         show2 = true;
        }
    Debug.Log("ITS ALIVE":)
 }
 else
 {
    Debug.Log("printed false :(");
   }
  }
 }
Comment

People who like this

0 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 Peterw301 · Nov 02, 2012 at 02:12 PM 0
Share

so far ive done this

function OnGUI () {

var position = Vector2(0,0); for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Stationary) { if (touch.position == touch) { (GUI.Box(Rect(0,0,100,20),"Example")); } } } }

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Michael CMS · Nov 03, 2012 at 02:31 PM

What you want to do is to test if the touch is on top of your npc :

 RaycastHit hit;
 Collider m_Collider
 if  (m_Collider.Raycast(
     Camera.main.ScreenPointToRay(
     new  Vector3( 
         Input.Touches[0].position.x,
         Input.Touches[0].position.y,
          0),
     out hit, 
     Mathf.Infinity))
 
 {
    //show gui  
 }


This is C# code, but you get the point :). Also this wasn't compiled , so you might need to test for some errors.

In the code above m_Collider is a collider component placed on your npc. This script should go on the NPC as well, and the code snippet above should go on the Update function.

Comment

People who like this

0 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 Peterw301 · Nov 13, 2012 at 03:08 PM 0
Share

thx i have fixed it now but the gui dissapeers when i let go :(

avatar image Itinerant · Nov 13, 2012 at 04:58 PM 1
Share

Add a boolean, and have the touch switch the boolean on when you touch it. When the boolean is true, GUI shows up. When it's false, it doesn't. Once you've gone through all the buttons you need, have the last one set the GUI boolean to false.

avatar image Peterw301 · Nov 14, 2012 at 06:57 PM 0
Share

Thanks @Itinerant im strugging with that though :( i will post where i am at the moment as an edit :)

avatar image

Answer by shaystibelman · Nov 13, 2012 at 04:58 PM

This is my TOUCH script. I have attached it to the player and get all my touch effects from there (that way I run the ray test only once). In your case you could attach it to a GUI that will check if it has been touched (maybe with a variable) in order to display the GUI.

 function Update(){
 #if UNITY_ANDROID
     if (Input.touchCount > 0) 
     {
         var theTouch : Touch = Input.GetTouch (0);
         touchPos = theTouch.position;
         touchPos.z = 1;
         var ray : Ray = cam.ScreenPointToRay(touchPos);
         var hit : RaycastHit;
         if(Physics.Raycast(ray,hit,1000.0))
         {    
             if (Input.touchCount == 1 && theTouch.phase == TouchPhase.Began/* && Target.collider.gameObject.tag=="Product"*/) 
                {
                 Target = hit.collider.gameObject;
                 TargetGrabFunction = Target.GetComponent(GrabNRotate);
                 TargetGrabFunction.bringOverInt = 1;
             }
         }
     }
     #endif
 }
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Button reaction time problem 1 Answer

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

Create a Button Scrollview 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