Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jamiemon · Apr 01, 2017 at 06:53 AM · javascriptcolliderscollision detection

How to detect when two gameObjects collide by name {java, 2d}

I've got a box & piece of terrain called "Area1grass". The box has a 2d box collider & the terrain has a 2d polygon collider. Both colliders are set to be triggers. This script below moves the box left & right using the arrow keys, it moves down automatically 0.019 & is supposed to move up by 0.02 when it collides with the terrain {it warps back to the top if it goes too far down}. It's just a simple test to become familiar with colliders:

 #pragma strict
 
 
 function Start () {
     
 }
 
 function Update () {
     if(Input.GetKey ("left")){
     transform.position.x = transform.position.x -0.1;
 } if(Input.GetKey ("right")){
         transform.position.x = transform.position.x + 0.1;
         }
 transform.position.y = transform.position.y - 0.019;
 if(transform.position.y < (-5)){
     transform.position.y = 6;
     }
 }
 
 function OnCollisionEnter(collider : Collider){
     if(collider.gameObject.tag == "Area1grass"){
         transform.position.y = transform.position.y + 0.02;
     }
 }

Everything else is running just fine except that the box falls through the terrain every time, I've been through countless questions/answers on here & none of them seem to work >.> Remember I'm not using rigidbodies or Unity's built in physics; I simply want to know how to detect when one gameObject is touching another. Any help is much appreciated ^-^

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kishotta · Apr 01, 2017 at 02:43 PM

You're close.

Put your collision logic in OnTriggerEnter2D instead.

Comment
Add comment · Show 3 · 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 Jamiemon · Apr 02, 2017 at 07:21 PM 0
Share

function OnCollisionEnter2D(col : Collision2D){ if(col.gameObject.tag == "Area1grass"){ transform.position.y = transform.position.y + 0.02; Debug.Log("finally"); } }

It still isn't doing anything >.< Is OnCollisionEnter the best function to use to check whether or not an object is touching another per every frame?

avatar image Kishotta · Apr 02, 2017 at 10:56 PM 0
Share

Typically, yes, but you are using trigger colliders which behave differently.

As well, you're using 2D colliders. There is a difference between OnCollisionEnter and OnCollisionEnter2D.

Try using this:

  function OnTriggerEnter2D (collider : Collider2D){
      if(collider.gameObject.tag == "Area1grass"){
          transform.position.y = transform.position.y + 0.02;
      }
  }
avatar image Jamiemon Kishotta · Apr 05, 2017 at 03:24 AM 0
Share

I added a rigidbody to the square, it now falls on top of the terrain & lands using the physics of the rigidbody, but I still can't get it to detect the collision in my script Here's the newest one:

 #pragma strict
  function OnTriggerEnter2D (col : Collider2D) 
  {
      if(col.gameObject.tag == "terrain1") 
      {
          Debug.Log("Test");
      }
  }

& just so you know, this is the entire script from top to bottom, if there's something I need to define before I wouldn't know what it is ^^;

avatar image
0

Answer by ManigandanR · Apr 03, 2017 at 06:51 AM

There are several points to keep in mind when you want to detect collisions.

  1. What Kind of Collider you are using? One of the 2D Colliders or 3D Colliders. If you are using 2D Collider, you can use methods like OnCollisionEnter2D or OnTriggerEnter2D only.

  2. Whether IsTrigger is checked on the Collider? IsTrigger specifies that an object can pass through a colliding object. If it is checked you can use methods like OnTriggerEnter2D or OnTriggerEnter.

Apart from these, there are pre-requisites to detect Collisions.

  1. A Collider component should be added.

  2. A Rigidbody component should be added.

  3. The Collision event handling method like OnCollisionEnter, OnCollisionStay, OnTriggerEnter, OnTriggerStay, OnCollisionEnter2D, OnCollisionStay2D, OnTriggerEnter2D or OnTriggerStay2D should be used.

Comment
Add comment · Show 2 · 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 Jamiemon · Apr 05, 2017 at 03:41 AM 0
Share
 #pragma strict
  function OnTriggerEnter2D (col : Collider2D) 
  {
      if(col.gameObject.tag == "terrain1") 
      {
          Debug.Log("Test");
      }
  }

This is the entire script. I added a rigidbody to the box, now the box lands on the terrain but I still can't even get it to print to the console >.> Is it because I had to uncheck IsTrigger in order to get the box to land?

avatar image ManigandanR Jamiemon · Apr 05, 2017 at 10:48 AM 0
Share

@Jamiemon You have to uncheck IsTrigger if you want your box to land. Checking the IsTrigger will make it to pass through ins$$anonymous$$d of landing. In that case, use OnCollisionEnter2D event. $$anonymous$$eep in $$anonymous$$d to change the parameter in Collision2D type.

Example: function OnCollisionEnter2D(col: Collision2D) { if(col.gameObject.tag == "terrain1") { Debug.Log("Test"); } }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How to check if 2 collisions are on the same objects ? (is player on the same ground than enemy?) 2 Answers

Complex collision layers 1 Answer

Trying to make object turn red OnTriggerEnter 0 Answers

Detect if a non-trigger collider is inside another collider 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