- Home /
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";
}
}
}
}
}
}
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
The content of the TapSelect function you have written should work fine. What GameObject are you attaching the script to?
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?
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
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.
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.
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.
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.
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";
}
}
}
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.