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 Jamie_h88 · May 12, 2014 at 11:18 AM · raycasttouch screen

Touch input not working

I am trying to create a button using a sprite with a box collider 2D attached. I have got it to work without using raycast but it is not very accurate. Now it does not do anything when I press the button.

I have used the documentation to put this together.

     var levelToLoad : String; 
     var newSprite : Sprite;
 
     
     
 function Update ()
 {
         for (var i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch(i).phase == TouchPhase.Began) {
                 // Construct a ray from the current touch coordinates
                 var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
                 if (Physics.Raycast (ray)) {    
         GetComponent(SpriteRenderer).sprite = newSprite;
         LoadNewLevel();
         Debug.Log("Touched");
             
             }
         }
     }
 }
         
 function LoadNewLevel()
 {
     yield WaitForSeconds(0.5);
     Application.LoadLevel(levelToLoad);
 }


Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jamie_h88 · May 12, 2014 at 01:01 PM

In the end I had trouble with both answers. Tested and Working now, it was much simpler than I was making it.

Here is the code that will allow a button to change sprite and load the level you want:

     var levelToLoad : String; 
     var newSprite : Sprite;
 
 
 function Update ()
 {
    var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
    
    if(Input.touchCount > 0)
    {
       if(hit.collider.name == "Button")
       {
      GetComponent(SpriteRenderer).sprite = newSprite;
      LoadNewLevel();
        
       }
     }
 }
 
 
 
 function LoadNewLevel()
 {
     yield WaitForSeconds(0.5);
     Application.LoadLevel(levelToLoad);
 }
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 HarshadK · May 12, 2014 at 11:39 AM

In your Physics.Raycast you need to specify the distance for the ray.

Refer: Physics.Raycast

So your if loop becomes

 if (Physics.Raycast (ray, 100)) {
   DoSomeStuff();
 }

Also if you want to detect the collider which the ray hit you can use:

 var hit : RaycastHit;
 
     if(Physics.Raycast(ray, hit, 100)) {
         print(hit.collider.name);
     }
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 Jamie_h88 · May 12, 2014 at 11:45 AM 0
Share

Thanks very much.

avatar image Jamie_h88 · May 12, 2014 at 12:14 PM 0
Share

Thought I had this sorted but it still does not seem to work. I have tried using physics2D but it is still not detecting the touch.

avatar image
1

Answer by Ashish Dwivedi · May 12, 2014 at 11:52 AM

You are using 2D collider that's why you have to use "Physics2D.Raycast()". I hope this would help you...

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

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

android touch optimization issue 1 Answer

Prevent Raycast on reacting to GUI input? 2 Answers

Why Raycast is not working properly? 0 Answers

Confused as to how my RaycastHit script works 1 Answer

Getting material index from raycast 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