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 RumplyThrower · Feb 08, 2014 at 12:39 AM · raycasttouchtagray

Raycast on touch

 var hit: RaycastHit;
 var manager : GameObject;
  
 function FixedUpdate()
 {
     if (Input.touchCount > 0) 
     { 
         var theTouch : Touch = Input.GetTouch(0);
         var ray = Camera.main.ScreenPointToRay(theTouch.position);
         if(Physics.Raycast(ray,hit,100))
         { 
             if(hit.transform.gameObject.tag == "planet")
             {
                 if(Input.touchCount == 1)
                 {
                     if(theTouch.phase == TouchPhase.Began)
                     {
                         var script : PlanetActionMenu;
                         script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
                         script.PlanetSelected();
                         script.planet = hit.transform.gameObject;
                         hit.transform.Translate(Vector3(1,0,0));
                     }
                 }
             }
             if(hit.collider.gameObject.name == "CameraMoveCollider")
             {
                  if(Input.touchCount == 1)
                 {
                     if (theTouch.phase == TouchPhase.Moved) 
                     {    
                         transform.Translate(theTouch.deltaPosition.x/-6,theTouch.deltaPosition.y/-6,0);
                         if(transform.position.x < 5)
                         {
                             transform.position.x = 5;
                         }
                         if(transform.position.x > 485)
                         {
                             transform.position.x = 485;
                         }
                         if(transform.position.y < 5 )
                         {
                             transform.position.y = 5;
                         }
                         if(transform.position.y > 485)
                         {
                             transform.position.y = 485;
                         }
                     }     
                 }
             }
         }
     }
 }

for some reason this never works:

             if(hit.transform.gameObject.tag == "planet")
             {
                 if(Input.touchCount == 1)
                 {
                     if(theTouch.phase == TouchPhase.Began)
                     {
                         var script : PlanetActionMenu;
                         script = manager.GetComponent(PlanetActionMenu) as PlanetActionMenu;
                         script.PlanetSelected();
                         script.planet = hit.transform.gameObject;
                         hit.transform.Translate(Vector3(1,0,0));
                     }
                 }
             }

i tried hit.collider.tag, hit.collider.gameObject.tag and hit.transform.tag. Nothing works, the 2nd if works just fine

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

Answer by Ashish Dwivedi · Feb 10, 2014 at 04:10 AM

Are you checking on editor? If you are checking on editor then this code would not work because on editor "Input.touchCount" will always be 0 and your first condition is "Input.touchCount > 0" that will remain false always on editor. So you have to write code so that work for both means on editor as well as on device.

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
0

Answer by xandermacleod · Feb 08, 2014 at 12:44 AM

try including the word 'out' before the word hit when firing your raycast. on line 10

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 rejwan1 · Feb 08, 2014 at 12:51 AM 1
Share

No need for that in JS

avatar image
0

Answer by rejwan1 · Feb 08, 2014 at 12:50 AM

  1. If you encounter such a problem, always use Debug.Log to try and figure out what's wrong & use MonoDevelop to debug break and go step by step. This way you can see what the raycasting actually hit (if it hit anything at all)

  2. You might have forgotten to tag your object correctly, give it a boxcollider or anything of the sort.

  3. Assuming it hit your object and we got into the 2nd "if" - the fact that you're doing this in FixedUpdate might be the source of the problem. A general rule of thumb is that you should only do physics related things in FixedUpdate. Input should be handled in Update.

  4. Another general rule of thumb is that if you want to get touch/mouse input, you should raycast at Mathf.Infinity - that way you will always "hit" something, even if it's far away

Comment
Add comment · Show 4 · 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 RumplyThrower · Feb 08, 2014 at 11:11 AM 0
Share

I'm using a spehre collider, could that be the issue? everything else should be fine, i checked for every possible error i could have done(distance for raycast is ok, tag is correct etc...) Btw thanks for replying

avatar image RumplyThrower · Feb 08, 2014 at 02:31 PM 0
Share

just tested with box collider, it doesn't work ...

avatar image xandermacleod · Feb 09, 2014 at 12:16 PM 0
Share

it should defo be collider.gameObject.tag so go that way for a start.

camera tag not set to mainCamera perhaps? theres very little here i can spot that should make it wrong with the script. more with your scene.

avatar image xandermacleod · Feb 09, 2014 at 12:17 PM 0
Share

after that id be grasping at straws like your Planet tag has a capital ins$$anonymous$$d of all lower case like in this script etc.

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

Exclude tag from raycast 1 Answer

Make tagged objects explode with ray cast? 1 Answer

This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer

Rays and tags help? 1 Answer

gameObject tag 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