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 MascarellDev · May 30, 2014 at 01:13 PM · 2dtriggertranslate

OnTriggerEnter2D not detecting

Hello! Its my first time here so i hope i can get help!, Im making an endless runner, i add the "infinite" background, and im adding also buildings, randomly positioned, and if one overlap with other, it recalculate his position, but does not work. I dont know where's the fail so... here we go

 using UnityEngine;
 using System.Collections;
 
 public class Buildings : MonoBehaviour {
 
     public float speed = -4.3f;
 
     void Update () {
         transform.Translate (speed * Time.deltaTime, 0, 0);
 
         if (transform.position.x < -18) {
             transform.Translate (Random.Range(35, 130), 0, 0);
         }
     }
 
     void OnTriggerEnter2D(Collider2D coll){
         if (coll.gameObject.tag == "build") {
             transform.Translate (Random.Range (35, 130), 0, 0);
         }
     }
 }

All the buildings have a box collider2d as a trigger, this code and the tag "build"

Comment
Add comment · Show 2
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 Nerevar · May 30, 2014 at 02:04 PM 0
Share

hi

I am not familiar with 2D collision but maybe try this function.

avatar image JayOhhh · May 30, 2014 at 02:05 PM 0
Share

Are you getting an error message of some kind?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by screenname_taken · May 30, 2014 at 02:23 PM

Hi. As far as i can tell, (at least with the 3D colliders i'm working with...) OnTriggerEnter (...) actually works only when something is entering the trigger when you run the game. So if something just pops/instantiates in the scene instead of waltz in, it won't register. Same with OnTriggerExit. Why i'm saying that? I'm making a game and has vanishing platforms. And when the platform vanished, OnTriggerExit didn't trigger to tell to the ball that it vanished.

So perhaps you can check with OnTriggerStay ? as that check to see if there is something in the trigger already instead of if it walked in.

Also, i think that unless you disable this code or check if the building is planted already and not being planted now, both buildings that touch each other might move away instead of the one coming in now.

What to try is to make all buildings take the same amount of space so that you have just a fixed value for distance. Save the place that the previous one was placed, and just add that fixed distance value to the transform of the new building that is to get placed.

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 MascarellDev · May 30, 2014 at 02:58 PM 0
Share

It works with OnTriggerStay2D, thanks!

avatar image screenname_taken · May 30, 2014 at 07:11 PM 0
Share

How do the buildings react btw? do both buildings try to move, or just the one that gets placed at that time?

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

22 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

Related Questions

Simple movement problem. Need units to stop dead on collision. 3 Answers

Move Object With Angle In 2D 1 Answer

Closed - Go to next scene on collision 1 Answer

Collision with renderer.enabled? 0 Answers

Ignore a collider when there are multiple ones? 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