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 am_012 · Aug 10, 2020 at 04:10 PM · collisiongriddetectionarea

Want to detect if object is in grid.

So I have an object and a script that generates grids. What I want to do is tell me if the object is in a specific area of the grid, and if so for how long. I looked up online for how to detect and found that to approach this with 2D colliders. So I attempted this and played the scene and ultimately it will not work. Is my code wrong. Both the object and the grid have isTrigger box checked on inspector. Just wanted to clarify I am new to unity. Here is the code and a picture of what is happening. alt text

Here is the code for telling if the object is in the grid(The debug also does not show when the object is in grid)

``` using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Collide : MonoBehaviour { bool isInSection1, isInSection2; private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.name == "GridHolder") { isInSection1 = true; isInSection2 = false; Debug.Log("Test");

      }
      else if (collision.gameObject.name=="eye")
      {
          isInSection1 = false;
          isInSection2 = true;
          Debug.Log("Test");
      }
  }

} ```

eyy.png (26.3 kB)
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 davidcox70 · Aug 10, 2020 at 05:18 PM

Add a rigidbody to your object and a collider set to trigger on the grid. For triggers to work, one of the objects has to have a rigidbody.

Comment
Add comment · Show 7 · 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 am_012 · Aug 10, 2020 at 05:22 PM 0
Share

the object has a rigidbody 2D already, so this will not work and i have to remove it and replace with rigidbody? Also do i remove box collider for the object and only keep it for grid

avatar image davidcox70 am_012 · Aug 10, 2020 at 05:24 PM 0
Share

If you already had a rigidbody2D, then maybe this isn't the problem. But set it up so you have a rigidbody2D and collider (not set to trigger) on your object, and a collider set to trigger on your grid, and see if that works. If not, report back and we'll look again.

avatar image am_012 davidcox70 · Aug 10, 2020 at 05:29 PM 0
Share

I did that and I still do not receive a debug log when the object is moved to the grid, perhaps something is wrong with my grid? Here is the code for the grid

using System.Collections; using System.Collections.Generic; using UnityEngine; //5,8,1 public class Grid$$anonymous$$anager : $$anonymous$$onoBehaviour { [SerializeField] private int rows = 5; [SerializeField] private int cols = 8; [SerializeField] private float tileSize=1; // Start is called before the first frame update void Start() { GenerateGrid(); }

private void GenerateGrid() { GameObject referenceTile=(GameObject)Instantiate(Resources.Load("GrassTile")); for (int row =0; row

            float posX = col * tileSize;
            float posY = row * -tileSize;

            tile.transform.position = new Vector2(posX, posY);
        }
    }
    Destroy(referenceTile);
    float gridW = cols * tileSize;
    float gridH = rows * tileSize;
    transform.position = new Vector2(-gridW / 2 + tileSize / 2, gridH / 2 - tileSize / 2);

} }

Show more comments

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

201 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 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 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

Enemy Detection Area 1 Answer

collision wont work 1 Answer

Collision isn't detected between two obects 1 Answer

How to detect if target is inside an "arc" area? 0 Answers

Detecting Trigger Collision for Mecanim Animator 0 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