• 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 /
  • Help Room /
avatar image
Question by ZombieTFK · Sep 07, 2016 at 12:16 AM · collision

Why is Collider.IsTouching not working in this sample?

Hey guys, I'm brand new to unity, and figured I'd get to grips with it by tackling the scripting API head on before anything else, but I've come across a bit of a stumbling block.

Here is my script, which is fairly straight forward. It's attached to an empty GameObject that in turn programmatically creates a couple of new GameObects (a green and a red box). The green box follows the mouse cursor around, while the red one sits there in the middle of the scene.

I wanted to test collision, so I added BoxCollider2D components to both objects, but when I compare them using IsTouching, the result is false. I was suscpisious of the bounds not being right, so I performed a manual collision check using the bounds.max and .min vectors, but that returned true... So now I'm very confused. What exactly is IsTouching checking for?? Why does it return false?

Here is my code :

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Game_Init : MonoBehaviour {
     GameObject player_cursor;
     List<GameObject> other_stuff;
 
     Game_Init(){
         other_stuff = new List<GameObject> ();
     }
 
     void Awake(){
         player_cursor = new GameObject();
         player_cursor.name = "Cursor";
         SpriteRenderer cursor_renderer = player_cursor.AddComponent<SpriteRenderer>();
         cursor_renderer.sprite = Resources.Load("green_box", typeof(Sprite)) as Sprite;
         player_cursor.AddComponent<BoxCollider2D>();
         /////////////////////////////////
         GameObject random_box = new GameObject();
         random_box.name = "random_box";
         SpriteRenderer box_renderer = random_box.AddComponent<SpriteRenderer>();
         box_renderer.sprite = Resources.Load("test/red_box", typeof(Sprite)) as Sprite;
         random_box.transform.localScale = new Vector3 (3, 3, 1);
         random_box.transform.position = new Vector3 (0, 0, 0);
         random_box.AddComponent<BoxCollider2D>();
         other_stuff.Add(random_box);
 
     }
 
     void Update() {
         BoxCollider2D collider =
             player_cursor.GetComponent (typeof(BoxCollider2D)) as BoxCollider2D;
         foreach (GameObject a_stuff in other_stuff) {
             //Debug.Log(other_stuff.Count);
             BoxCollider2D a_stuff_col =
                 a_stuff.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D;
             if(collider.bounds.max.x > a_stuff_col.bounds.min.x
                 && collider.bounds.min.x < a_stuff_col.bounds.max.x
                 && collider.bounds.max.y > a_stuff_col.bounds.min.y
                 && collider.bounds.min.y < a_stuff_col.bounds.max.y){
                 Debug.Log("is inside of box " + (new System.Random()).Next(1,10000));
             } //worky
             if(collider.IsTouching(a_stuff_col)){
                 Debug.Log("Contact!");
             } //no-worky
 
         }
 
         player_cursor.transform.position =
             new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                         Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
                         0
             );
     }
 }
 
Comment

People who like this

0 Show 0
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you detect a mouse button click on a Game Object? C# 2 Answers

Destroying GameObject on collision Problems (Begginer) 1 Answer

OnCollisionEnter2D is called after object was shifted 1 Answer

orbiting mouse controlled camera goes through terrain 0 Answers

Deleting an object from a list of objects from another script. 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