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 frodoe7 · May 08, 2015 at 07:57 PM · inputtouchevent

detecting touch on gameObject

Hello, I have 2 gameObjects called (right , left) I need to execute some actions in case of touching one of them but it does not detect any touch input , take a look of the below code :)

 public class player : MonoBehaviour {
     public float speed = 3;
     public GameObject left; //assigned via inspector
     public GameObject right; //assigned via inspector
     private Animator anim;
     // Use this for initialization
     void Start () {
         anim = gameObject.GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 scale = transform.localScale;
         if (Input.touchCount > 0) {
             Touch t = Input.GetTouch(0);
             if (t.phase == TouchPhase.Began)
             {
                 if (left.GetComponent<SpriteRenderer>().bounds.Contains(t.position))
                 {
                     anim.SetInteger ("walk", 1);
                     transform.Translate (-speed * Time.deltaTime, 0, 0);
                     scale.x = -1;
                 }
                 else if (right.GetComponent<SpriteRenderer>().bounds.Contains(t.position))
                 {
                     anim.SetInteger ("walk" , 1);
                     transform.Translate(speed*Time.deltaTime,0,0);
                     scale.x = 1;
                 }
             }
             else if (t.phase == TouchPhase.Ended)
             {
                 anim.SetInteger ("walk" , 0);
             }
         }
Comment
Add comment · Show 5
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 fighder · May 08, 2015 at 08:51 PM 0
Share

I just need some clarification. What exactly are the game object left and right? Are they colliders on the screen?

Also, you don't have to use Input.touchCount if your colliders are huge and you don't require precise coordinate of the figures, you can just use the normal On$$anonymous$$ouseDown stuff.

avatar image Infinite_Gamer · May 09, 2015 at 01:41 AM 0
Share

You could use void OnCollisionEnter

avatar image fighder · May 09, 2015 at 01:48 AM 0
Share

OnCollisionEnter doesn't even deal with touch or click input -.-

avatar image Abdou23 · May 09, 2015 at 05:46 AM 0
Share

You could also use "On$$anonymous$$ouseDown" and it will work as a touch input.

avatar image frodoe7 · May 09, 2015 at 06:12 AM 0
Share

@fighder take a look of this images http://imgur.com/HgvJPqI

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fighder · May 09, 2015 at 02:42 PM

You should have a script component for each "left" and "right" gameObject.

Inside those scripts, use the OnMouseDown function and execute your code inside the function.

i.e.

 void OnMouseDown(){
 anim.SetInteger ("walk", 1);
 transform.Translate (-speed * Time.deltaTime, 0, 0);
 scale.x = -1;
 }

to execute code when the user lifts their finger off the screen, use OnMouseUp instead.

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 frodoe7 · May 09, 2015 at 03:20 PM 0
Share

the problem that I did not add a collider component no more no less thanks for your comment at all

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about user input event handling in Unity game for iOS 2 Answers

Force touch to only work on a new touch - buttons 0 Answers

solution to detect multi- finger touch position 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

mouse input to touch input help please 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