I’m rather new to scripting for mobile and the best solution I have found for detecting if an object is being touched is this:
if(Input.touchCount > 0) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, hit, distance)){
if (hit.collider.gameObject == something)
}
}
However, I have 50 objects that can be touched. This means that 50 rays are being cast.
Why do you need to do the raycast 50 times? Even if there’s no way of detecting the correct object other than using an if-statement, why can’t you just put it all under the first raycast?
if(Physics.Raycast(ray, hit, distance))
{
switch(hit.collider.gameObject)
{
case something:
{
// Do things
break;
}
case somethingElse:
{
// Do other things
break;
}
}
}
(and so on)
Or, if you have an array of touchable objects, you can do something like this-
if(touchableObjectCollection.Contains(hit.collider.gameObject))
{
// do stuff
}
(syntax is obviously dependent upon your exact implementation).
Yes, that's what I'm suggesting. If you have one master script that handles all touches, it can save you a lot of time.
Yes, that's what I'm suggesting. If you have one master script that handles all touches, it can save you a lot of time.
– syclamoth