Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gintechsystems · Feb 25, 2015 at 06:13 AM · 2draycastvelocitygravitymass

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);
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gintechsystems · Mar 02, 2015 at 12:57 AM 0
Share

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?

avatar image smallbit · Mar 02, 2015 at 04:52 AM 0
Share

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) ?

avatar image gintechsystems · Mar 02, 2015 at 05:43 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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 do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded? 0 Answers

How to turn off gravity? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges