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 Double Panic · Aug 27, 2013 at 09:44 PM · c#raycasttagraycasthittouchscreen

C# change an object tag wit raycasthit.

hi I'm new to unity and I'm building an ios game in C#. all I would like to do is toggle a game object tag with touch input. I’m using a raycast (I got the script form another thread). I managed to get it working with a send message but have since found out that they are a bad habit to get in to. I have tried this but now touch inputs do nothing what so ever. I would really appreciate any advice or solutions. Is there an easier way to do this? Have I miss understood the whole situation? Thanks in advance.

 void Update () {
        TapSelect(); 
 }
  
 void TapSelect() {
    foreach (Touch touch in Input.touches) {
         if (touch.phase == TouchPhase.Began) {
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             RaycastHit hit ;
             if (Physics.Raycast (ray, out hit)) {
                        if(hit.collider.tag == "objectOff")
                        { 
                         hit.collider.tag = "objectOn";
                        }
                         else {
                          hit.collider.tag = "objectOff";
                       }    
                    }
                 }
             }
         }
     }
 
Comment
Add comment · Show 8
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 getyour411 · Aug 27, 2013 at 10:33 PM 0
Share

Try putting a debug.log just before hit.collider.tag="objectOn"; to ensure you are at least getting there, I don't know this platform but that would at least confirm the use of Touch and the foreach/if touchphase stuff is working

avatar image TrickyHandz · Aug 27, 2013 at 10:35 PM 0
Share

The content of the TapSelect function you have written should work fine. What GameObject are you attaching the script to?

avatar image Double Panic · Aug 28, 2013 at 10:35 PM 0
Share

thanks for the responses. it's attached to a sphere collider. and I can't run debug.log because I'm testing on an iPhone so as far as I'm aware I can't see the log. however I have just realised that is is detecting touch just off to the side away from the game object. why would that be? any ideas, can I re-center the detection?

avatar image TrickyHandz · Aug 28, 2013 at 10:45 PM 0
Share

Double Panic, have you looked at using Unity Remote for your testing? It may help you out for testing on your iPhone. Here's a link to the docs on it: Unity Remote Here is the AppStore link as well: Unity Remote 3 App

avatar image Double Panic · Aug 28, 2013 at 11:26 PM 0
Share

thank you that app is very useful! will save me allot of time however it has just confirmed that the touch input is being registered away from the game object.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Aug 29, 2013 at 09:35 PM

thanks for the responses. it's attached to a sphere collider. and I can't run debug.log because I'm testing on an iPhone so as far as I'm aware I can't see the log. however I have just realised that is is detecting touch just off to the side away from the game object. why would that be? any ideas, can I re-center the detection?

your doing

Camera.main.ScreenPointToRay(touch.position)

are you sure touch.position is a screenpoint?

A screenpoint is the pixel location but perhaps touch.position is a world position so its distance from the global origin not its screen position.

If so the use of

camera.main.ScreenPointToRay(camera.main.WorldToScreenPoint(touch.position));

may be the appropriate way to solve this.

My guess is basically that touch.position is a viewport point or a world point and not a screenpoint.

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 Double Panic · Sep 01, 2013 at 12:59 AM 0
Share

humm no pretty sure touch.position is a screenpoint. thank you very much for the idea but the problem defiantly isn't with the raycast because debug.log is returning results when the object is tapped. as far as I can see the only problem is changing the tag on only the object that has been hit. can't seem to get it working.

hit.collider.transform.tag

doesn't work and I cant think why.

avatar image Jamora · Sep 01, 2013 at 01:49 AM 0
Share

Have you tried using hit.transform.gameObject.tag? $$anonymous$$aybe transform.tag and gameObject.tag aren't the same thing for the same GameObject.. who knows.

avatar image
0

Answer by citizen_rafiq · Sep 01, 2013 at 03:26 AM

//first you need to write down both tag in tag manager

 private Ray ray;
 private RaycastHit hit;
 
 void Update()
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          
     if(Physics.Raycast(ray,out hit, Mathf.Infinity) && Input.GetMouseButtonDown (0))
     {
         
         if(hit.collider.gameObject.tag == "Wood")
         {
             hit.collider.gameObject.tag="Water";
         }
     }
 } 
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 Double Panic · Sep 03, 2013 at 11:53 PM 0
Share

ok, this woks perfectly!. thank you very much do you have any suggestions as to how I can make the tags switch again? I have tried

        if(hit.collider.gameObject.tag == "Wood")
        {
          hit.collider.gameObject.tag="Water";
          Debug.Log ("touchOn");
        }
 
       else if(hit.collider.gameObject.tag == "Water")
       {
          hit.collider.gameObject.tag="Wood";
          Debug.Log ("touchOff");
       }

but as the console shows more than one input is being registered and the script is run more than once each time. so the tags (I assume) are being switched more than once.

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

19 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

Related Questions

How can i use raycast hit to detect object by the object tag name ? 1 Answer

Getting a Raycast to Classify Objects as the Same Thing 0 Answers

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag, and which should I use when? 1 Answer

Raycast executing hundreds of times 2 Answers

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 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