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 g3tinmybelly · Jul 16, 2014 at 03:42 AM · collisioncollisiondetectioncollider 2d

Problems with clicking an object with my script

So I have a seriously dumb issue with my script where you have to click the bottom of the object (circle in this case) to get the object to disappear but your suppose to be able to click any part of the circle for it to disappear. There seems to be a problem with collision precision. By the way, this is built for mobile devices. Here is the code to give you an idea.

 using UnityEngine;
 using System.Collections;
 
 public class FalloutBallScript : MonoBehaviour {
 
     public float terminalVeclocity = -10;
 
     private CircleCollider2D clickCollider;
 
     private bool isBomb = false;
 
     private int score = 1;
     private float speed;
 
     void Start()
     {
         clickCollider = gameObject.AddComponent<CircleCollider2D>();
         clickCollider.radius = gameObject.GetComponent<CircleCollider2D>().radius * 1.4f;
         clickCollider.isTrigger = true;
         float randNum = Random.Range(0f, 1f);
         if (randNum <= .1) isBomb = true;
         if (isBomb)
         {
             renderer.material.color = Color.red;
             gameObject.tag = "Bomb";
         }
         rigidbody2D.gravityScale = Random.Range(.85f, 1.1f);
         Vector2 newVel = new Vector2(Random.Range(-1f, 1f), rigidbody2D.velocity.y);
         speed = Random.Range(2f, 8f);
         rigidbody2D.velocity = newVel * speed;
     }
 
     void FixedUpdate()
     {
 
         if (!FalloutGameControllerScript.gameOver)
         {
             foreach (Touch t in Input.touches)
             {
                 Vector2 tPos = Camera.main.ScreenToWorldPoint(t.position);
 
                 if (t.phase == TouchPhase.Began)
                 {
 
                     if (clickCollider.bounds.Contains(tPos))
                     {
                         if (isBomb) FalloutGameControllerScript.gameOver = true;
                         else
                         {
                             FalloutGameControllerScript.score += score;
                         }
                         Destroy(gameObject);
                     }
                 }
             }
 
             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             if (Input.GetMouseButtonDown(0))
             {
                 if (clickCollider.bounds.Contains(mousePos))
                 {
                     if (isBomb) FalloutGameControllerScript.gameOver = true;
                     else
                     {
                         FalloutGameControllerScript.score += score;
                     }
                     Destroy(gameObject);
                 }
             }
         }
 
         if (rigidbody2D.velocity.y < terminalVeclocity)
         {
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, terminalVeclocity);
         }
 
     }
 
 }
 
Comment
Add comment · Show 6
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 robertbu · Jul 16, 2014 at 04:07 AM 0
Share

If you look at the colliders in Scene view while the app is running, are the colliders appropriate aligned with the object? I assume you are using an Orthographic camera?

avatar image g3tinmybelly · Jul 16, 2014 at 05:30 AM 0
Share

Ya, I am using an Orthographic camera and the colliders seems to be perfectly aligned. I am honestly really confused.

avatar image robertbu · Jul 16, 2014 at 05:50 AM 0
Share

You don't have multiple camera do you? Does it fail on both the touch device and with the mouse?

avatar image g3tinmybelly · Jul 16, 2014 at 03:03 PM 0
Share

No, Only one camera per scene and it seems to work better with the mouse then it does with touch.

avatar image robertbu · Jul 16, 2014 at 05:29 PM 0
Share

First, are you sure you want 'bounds.Contains' rather than 'OverlapPoint'? Since the collider is 2D, the bounds will have 0.0 for the 'z' extent. This is probably okay, but something you might want to look into if your problems persist.

Taking a closer look, is there any chance that it is not a collider issue but a lag between the mouse/finger hardware detection and the falling object position? Comment out lines 27 and 30, or drastically reduce the gravity and velocity. See if slow moving or not moving objects suffer from the same problem.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Obstacle avoidance 1 Answer

collision detection not working? 1 Answer

Collision detection for sprite 1 Answer

How does Unity detect collision? 0 Answers

Getting sprites to stop going through one another? 1 Answer


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