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 mrgeorge3d · Sep 26, 2014 at 11:21 PM · 2dphysicsraycasttouch

Inaccurate Touch Detection iOS

I have a Camera that follows my Player, so I want the user to "Tap" "Coins" to make them fall. I am using Physics2D.Raycast to detect a collision between a circle collider and a touch Position. The script works, but it is very difficult to "Tap" the coins, specially when the player is running because they move faster. Trying to fix this, I made my circle Collider Radius Bigger than my actual Sprite, It worked a bit better, but when my player moves fast, again does not work well, and also I realized that if I tap on the left side of the coin (Outside the Collider) that will hit my collider. What am I Doing Wrong, or Why does the detection is slow?

Im Testing this on my iPad Mini, but when I test on a smaller device like an iPhone, it gets worst.

This is my script Attached to the coins GameObjects:

 using UnityEngine;
 using System.Collections;
 
 public class Touch : MonoBehaviour {
 
 public RaycastHit2D hit;
 private LayerMask coinLayer;
 
 
 void Awake()
     {
         layerJitomates = 1 << LayerMask.NameToLayer ("Coins");
     }
 
 
 
 void Update () 
     { 
         
         if (Input.touchCount > 0)
 
 
 
             for(int x =0;x<Input.touchCount;x++)
         {
             hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(x).position),Vector2.zero,coinLayer);
 
 
 
 
 
 
             if(hit != null && hit.transform != null && hit.collider.tag == "Coin")
             {
                 if(Input.GetTouch(x).phase == TouchPhase.Began)
                 {
 
                     hit.rigidbody.isKinematic=false;
                     hit.rigidbody.collider2D.GetComponent<CircleCollider2D>().radius = 1.65F;
                     hit.rigidbody.collider2D.GetComponent<CircleCollider2D>().isTrigger = false;
 
                 }
 
 
             }
         }
         
         
         
     }
 
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 26, 2014 at 11:41 PM

This is most likely a matter of execution order. You probably move your player / coins also in an Update function, right? If you have high speeds the player / coins move quite a bit each frame. Depending on what is executed first (movement or raycasting) you might lag one visual frame behind.

Take a look at the Script Execution Order. You should make sure that you execute the raycast-script before the movement script. That's because at the beginning of a frame you see (visually) the result of the last frame. So doing the raycasting there should be exact.

If you do the raycasting after the movement the objects have moved already, but aren't displayed until the end of the current frame.

Comment
Add comment · Show 1 · Share
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 mrgeorge3d · Sep 27, 2014 at 12:39 AM 0
Share

Thanks Bunny83, I thought the same, but I made the "Script Execution Order" thing, and its happening the same, $$anonymous$$aybe I'm doing something else wrong, but thanks for your help... I'll keep looking for answers.

Thank you!

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 DO I USE RAYCASTHIT 2D???? 1 Answer

Linecast, rotate end point relative to objects rotation 1 Answer

2D Slope acceleration 1 Answer

Detect touched 2d object in Unity3d 4.3 1 Answer

Raycasting not behaving as expected? 2 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