- Home /
Keep up with falling object - Touches
Hello,
I am working on a game where objects fall from the top of the screen. I have them working with raycasting. The game is 2D. The issue I am having is if they are going too fast, even if you tap on them, they do not ALWAYS get detected by the raycast. How can I keep up with the speed of a falling object? I have tried boxcast & circlecast but they seem to only work if you tap far ahead of the falling object. I have adjusted mass, drag & gravity scale. It seems if I do anything faster than 10 gravity scale the raycast is very hard to keep up with the touch positions.
Here is some of my code just to make it more understanding.
if (Input.touchCount > 0) {
if (Input.touchCount == 1) {
Touch currTouch = Input.GetTouch(0);
if (currTouch.phase != TouchPhase.Began || currTouch.phase != TouchPhase.Ended)
{
return;
}
CheckForSplat(currTouch.position);
}
else {
for (int i = 0; i < Input.touchCount; i++) {
Touch currTouch = Input.GetTouch(i);
if (currTouch.phase != TouchPhase.Began || currTouch.phase != TouchPhase.Ended)
{
return;
}
CheckForSplat(currTouch.position);
}
}
}
void CheckForSplat(Vector3 pos) {
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
}
randomVelocity = Random.Range(-200.0f, -300.0f);
newDroplet.GetComponent<RectTransform>().SetParent(mainCanv.transform);
newDroplet.transform.SetAsLastSibling();
newDroplet.transform.localScale = new Vector3 (1.0f, 1.0f, 1.0f);
newDroplet.rigidbody2D.mass = 0.25f;
newDroplet.rigidbody2D.drag = 0.0f;
newDroplet.rigidbody2D.gravityScale = 10.0f;
newDroplet.rigidbody2D.isKinematic = false;
newDroplet.rigidbody2D.velocity = new Vector2(0, randomVelocity);
newDroplet.rigidbody2D.AddForce(spawnerPosition.forward, ForceMode2D.Impulse);
I do have some ideas on how to calculate the distance needed, but not sure how to get the object that the touch is closest too I suppose you can say?
What FPS are you running your game at? Do you control in which order those two actions (moving and raycast) are happening? I had a similar problem once and it was due to object moving too much per frame was actually "jumping over" the place it was supposed to collide, i increased the collider size and forced game to run 60fps as oppose to previous 30. Have you tried to force for every frame first moving object and than calculating raycast (in script execution order) ?
I have tried increasing the collider size but don't want to do it too much because then they objects will start touching each other and bouncing off each other, right? I check input touches in void update(), if one is found then I calculate the raycast as mentioned above. If you wish, I can show you both script functions. How can I check what the current FPS is? Is there a way to turn on a counter or do I have to create a script? That sounds like it may be a problem though, these objects get pretty fast at some point (I am talking like with over 1,000 velocity). I tried to lower the gravity scale from 10 to 1 and raising the velocity ins$$anonymous$$d, seemed to help. I think part of the problem may be also that it works fine when I use the mouse on my PC but when I use a real device it feels off, I thought ScreenToWorldPoint works for both mouse and touch positions and any screen size, they should produce the same? I am creating a 2d game.
Your answer
Follow this Question
Related Questions
how to jump at fixed height but faster 0 Answers
Simulate gravity on rigidbody 1 Answer
Can I emulate simple space-like gravity? 2 Answers
How to turn off gravity? 4 Answers