- Home /
iOS: how do I make an empty area a tappable target: Ring
I've made a 3D model of a ring and want the entire inside area of the ring, as well as it's frame, to register any taps inside or on the rim. Problem is, there's nothing in the center of the ring to do a raycast on, and no object for me to put a listener on for contact/touch in this middle/empty area of each ring. And that's the way I want it. I don't really want any more geometry or stuff flying around to deal with.
What's the most efficient way of solving this problem if there's a few of these rings on the screen at the same time and I want to differentiate between them; Some being the "right" ones to tap, some being the "wrong" ones.
Because I'm useless at explaining myself: Here's an image: so you know what I'm on about... http://imgur.com/1hJvh
And just image a half dozen of these floating around the screen.
Answer by AngryOldMan · Mar 17, 2011 at 02:06 PM
make the rings as prefabs with a collider on them (sphere or capsule only)with "is trigger" checked which reacts when "input.touches" is added in the script attached. This means you would have two prefabs that both looked the same but one had a script for "right" touch and one having a script for "wrong". Simple, no need for raycast this way :)
which of these methods is the most processor efficient?
I think that the input.touch feature will be less processor intensive but I'm not 100% sure. I know it's simpler and less complex to implement. $$anonymous$$y general motto is less code less memory but that is, definatly, not always the case!
wouldn't this mean that the number of objects I have onscreen equals the number of eventlisteners are operating? And that event listeners are processor drains in iOS? Whereas the ray approach only sends a ray on touch, and can use just one event listener, for the whole screen?
Like I said I wasn't 100% sure but the way you explain sounds right. But i don't think you need an event listener at all with input.touches?
Answer by pyro 2 · Mar 17, 2011 at 02:03 PM
Add a SphereCollider to your ring, center it in the middle of the ring and adjust the radius so it envelopes the whole ring.
Then just use a a raycast from the camera to see if it is hitting one of your colliders.
function Update() { //check touches if (Input.touchCount > 0) { //grab touch var t = Input.touches[0];
//check if it is the beginning of a touch
if (t.phase == TouchPhase.Began)
{
//shoot a ray from the touch position into the scene
var ray = Camera.main.ScreenPointToRay(t.position);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) //check if the ray is hitting something
{
//destroy whatever was touched (modify this to your own gamelogic)
Destroy(hit.transform.gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
Unresponsive Touch 0 Answers
iOS :: increase slider thumb touch area 1 Answer
Multitouch Area 1 Answer
Help With Touch to Drag Script 1 Answer