hi im still trying to learn the best way of raycasting, im currently using it with my own gui system and was wandering which method is the best perfomance wise to cast a ray?
so at the moment my script looks like this
for(var i = 0; i <Input.touchCount; ++i){
var touch = Input.GetTouch(i);
var ray = Camera.mainCamera.ScreenPointToRay (touch.position);
var hit : RaycastHit;
var guiLayer = 1 << 10;
if(Physics.Raycast (ray,hit,Mathf.Infinity,guiLayer)){
// Debug.Log(hit.collider.gameObject.name);
if(hit.collider.gameObject == Object){
do stuff
}
}
}
but i have seen another method of using switch,case any advice
For 3D touches I have an IInteractiveObject interface that other scripts can implement to handle touch events and instead of comparing objects and then ‘doing stuff’ I look for the component on the collider and if present I call the relevant method, effectively delegating the behaviour of that touch action to the object that was touched (or the type rather, as I have only a handful of interactive object types). Although I use this for 3D, you could use the same kind of thing for UI.
Apologies as this is C# code - I don’t know the equivalent javascript.
As you’re only ray-casting a particular layer, you probably want all colliders in that layer to be considered, so it is likely to make little difference having infinity or a particular depth. If you extend to ray-casting into the 3D view, then you probably want to be able to configure which cameras to use, which layers to consider and how deep the touch should go.
As an aside…
For your GUI, it is a common practise to create a texture atlas so that you can (in theory) draw the whole GUI in 1 draw call - this is a very good way to improve performance on mobile as there are fewer draw calls and state changes (changing the material for each button for example). I don’t know if you’re doing this?