• 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 ved_verma · Oct 09, 2014 at 07:10 PM · android

Can Android touch and GuiTexture click work simultaneously?

Heyy Unity expert, need some help. I am making a game where I need to work with both touch and GUITexture click simultaneously. What I am doing is I've divided screen width into two parts, in left part(that is 35% of whole width), if user touch above the player it will move in upward direction and player move downward direction if user touch below the player. And In right part user can touch anywhere to move player in forward direction. Othan than this I want a GUITexture in left part of screen where user can tap and I can perform some functionality on that. Now the problem is click functionality on GuiTexture is not working only touch is working even if I click on GuiTexture also.

Below is the script that is associated to my player:

using UnityEngine; using System.Collections;

public class TestPlayerTouch : MonoBehaviour {

 public float speed = 400f;
 Rigidbody2D myBody;
 Vector2 movement;
 private SpriteRenderer spriteRenderer;
 public bool isBlueColor = true;
 public bool isGameComplete = false;
 public Vector3 playerPosition;
 public GUIText touchText;
 public GUIText playerPositionText;

 // Use this for initialization
 void Start () {

     myBody = this.rigidbody2D;
     spriteRenderer = GetComponent<SpriteRenderer>();
 }

 


 // Update is called once per frame
 void Update () {

     if (myBody != null)
     {

         //Works for keyboards and joysticks
         #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT

             MoveHorizontally(Input.GetAxisRaw("Horizontal"));
             MoveVertically(Input.GetAxisRaw("Vertical"));
             if (Input.GetButtonDown("Jump"))
             {
                 toggleColor();

             }

         #endif

         playerPosition = myBody.transform.position;         
         

         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {

             Vector2 touchPosition = Input.GetTouch(0).position;
             

             if (touchPosition.x >= 0 && touchPosition.x <= Screen.width * .35f)
             {

                 if (touchPosition.y > playerPosition.y)
                 {

                     MoveVertically(1);

                 }
                 else if (touchPosition.y < playerPosition.y)
                 {

                     MoveVertically(-1);

                 }

             }
             else {

                 MoveHorizontally(1);

             }

         }
     }
 }
 public void MoveHorizontally(float horizontal_input)
 {
     if (horizontal_input > 0 && myBody != null)
     {

         movement = myBody.velocity;
         movement.x = horizontal_input * speed * Time.deltaTime;
         myBody.velocity = movement;
     }
 }

 public void MoveVertically(float vertical_input)
 {

     if (myBody != null)
     {

         movement = myBody.velocity;
         movement.y = vertical_input * speed * Time.deltaTime;
         myBody.velocity = movement;
         
     }

 }

}

Below script is associated to my GUITexture :

 using UnityEngine;
 using System.Collections;
 
 public class ToggleColor : TouchLogicV2
 {
     TestPlayerScript player;
     void Start()
     {
         player = FindObjectOfType<TestPlayerScript>();
     }
 
     public override void OnTouchBegan()
     {
         player.toggleColor();
     }
 
     
 }

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

Answer by Graham-Dunnett · Oct 09, 2014 at 07:12 PM

Probably better to just use Input.GetTouch() and then use the touch position to work out if the user has touched the GuiTexture.

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Copy mp3 to cellphone 0 Answers

(Mobile) Touch anywhere on screen EXCEPT button 3 Answers

How many different scripts do I need to write to create a highscore system? 1 Answer

Conflict between AndroidNative library and android Manifest 1 Answer

How do I access CoreMotion and the Android equivalent? 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