- Home /
Touching Falling Objects
Hi, here's my problem. I have cubes spawned at the top of the screen and falling down because of gravity. Touching a cube makes it disappear and increase the score. it works pretty good but as the speed gets higher the touch seems not working.
I've already tried editing the timestep values and other time settings in the Edit>project settings>Time tab, without any good result.
here's my code, any help is appreciated.
foreach (Touch t in Input.touches) {
if (t.phase != TouchPhase.Moved && t.phase != TouchPhase.Stationary) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (t.position);
if (Physics.Raycast (ray, out hit, 100f)) {
if (hit.collider.gameObject.tag == "cube") {
//SCORE UPDATE AND DESTROY() HERE
}
}
}
}
One of the reason for this to happen is that since physics and rendering are independent your object might not be physically at the place where it is rendered if it is moving at great speed.
Can you use Debug.Log to check the position of your cube and related positions for input? That might help understanding the problem better.
I don't think that should happen very often. In order for the physics system to do that you would need to be rendering @ < 60FPS.
Answer by darthbator · Jun 10, 2014 at 11:26 PM
Sometimes when I deal with something like this I will end up casting a sphere and not a ray. This kind of allows you do hit a "broader" spectrum of world space then a ray.
http://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
I find lots of touch input to be pretty imprecise. Think about it when you put your finger down you are touching WAY more then 1 screen coordinate location. I often find the circle or sphere casts/checks to be a better representation of a players actual touch then a ray is.
the code is now as follows: foreach (Touch t in Input.touches) { if (t.phase != TouchPhase.$$anonymous$$oved && t.phase != TouchPhase.Stationary) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (t.position); if (Physics.SphereCast (ray,1,out hit,100)) { if (hit.collider.gameObject.tag == "cubo") {
but still, it looks like it's really imprecise and it misses the cubes quite often as they become faster. the FPS rate is over 50 for the duration of all the game.
Your answer
Follow this Question
Related Questions
Falling object touch 0 Answers
raycast hit add force problem, help needed please 1 Answer
Detect if dragged over object 1 Answer
Getting raycast to fire at touch position but only when second finger is on screen 0 Answers
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers