- Home /
Raycast Hit not working
Hello
We made a script that toggles the camera between different iTween paths when you click objects on mobile
But the Recast isn't reliable with multiple buttons I even tagged the clickable objects
Here's script: Thanks
public class ControlSampleRayA : MonoBehaviour {
//NOTE: You have to TAG your Collider with "trigger_a"
public int pathNo=0;
public int path2Return2 =0;
public float percentage=0f;
float returnPercentage;
public string debugLog="";
public string trigger = "trigger_a";
Camera camera;
FlythroughCamera_Mod aController;
public Collider coll;
private RaycastHit hit;
// Use this for initialization
void Start () {
camera = Camera.main;
aController= camera.GetComponent<FlythroughCamera_Mod>();
coll = GetComponent<Collider>();
}
void Update() {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
RaycastHit hit;
if (coll.Raycast (ray, out hit, 100.0f)) {
if (hit.collider.gameObject.tag == trigger) {
if (aController.whichPath != pathNo){
returnPercentage=aController.percentage;
Debug.Log (debugLog+" "+ aController.whichPath+" "+ pathNo+" "+ returnPercentage);
aController.ToggleToPath (pathNo, newPercentage:percentage);
}
else
{
Debug.Log (debugLog+" "+ aController.whichPath+" "+ pathNo+ " "+ returnPercentage );
aController.ToggleToPath (path2Return2,newPercentage:returnPercentage);
}
}
}
}
}
}
this form of Raycast: coll.Raycast (ray, out hit, 100.0f)
will detect a hit upon collider "coll", and no others. Since it looks like this collider does NOT actually belong to the objects you are clicking, you are never going to get a hit on those objects, unless by happenstance.
I think you want to use the physics.raycast version of this function to see if it has hit ANY of your clickable objects: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Thanks Glurth!
but I define both the public variables "trigger" & "coll" in the inspector with each instance of the script, so that if the collider.gameObject.tag is such it should execute...?
Right?
if (coll.Raycast (ray, out hit, 100.0f)) {
if (hit.collider.gameObject.tag == trigger)
I tried it with physics.raycast but it's not working for me.
I am testing it in Unity Remote and it works / but not in builds
Thanks
but I define both the public variables "trigger" & "coll" in the inspector with each instance of the script, so that if the collider.gameObject.tag is such it should execute...?
We don't have enough information to answer that, yet. If you are creating a separate instance of the ControlSampleRayA class for each object you $$anonymous$$IGHT click on, then: YES
But I don't think that's what you are doing there... I think you have one of those classes defined for each select-able camera position. So, each ControlSampleRayA can have only one object's collider to checked against in your code. All the other clickable objects would be ignored.
If you use the collider.Raycast version then you $$anonymous$$UST compare it against every possible collider. Doing this is certainly feasable, and often more efficient than the physics raycast. But you need to keep you own list of colliders to check against. Note: this also eli$$anonymous$$ates the need for using & checking tags, since you can just consider only colliders on the list you check, are "tagged".
Thanks Glurth for taking the time.
Lets see if I can clarify this better, $$anonymous$$y camera is following a main path and along the way it passes a bunch of boxes each with a collider & also the ControlSampleRay script on it, if at anytime, you click on a distinct box the camera changes to another distinct path...
The other path is a loop and returns to the box, clicking on that box again returns it back to the main path
Since i have a bunch of colliders and I need these to each trigger specific paths I thought the coll.Raycast would be the better option...
When I try using physics.raycast it seems like anything I touch triggers path switches
Your answer
Follow this Question
Related Questions
Help With Touch to Drag Script 1 Answer
Dragging an object in iOS (UnityScript) 3 Answers
Detect the swipe and add force respective to it? 3 Answers
touch game objects on ios 1 Answer
Multiple Cars not working 1 Answer