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 /
  • Help Room /
avatar image
0
Question by Pajgla · Oct 24, 2016 at 03:30 PM · collider2d gametrigger

Problem with 2D Triggers

Hi, i am working on this game for a while now, and i have one big problem with 2D Triggers. So, let me explain how my trigger should work : i have a block-based game. In the front of the player, there is 2D Trigger that activates when block (with collider) is inside the trigger. When block enters in that area, block changes color to red, and the collider of that block is stored inside variable (for calling functions like "Mine") and variable isBlockSelected changes it's value to true. The only problem here is that, if player rotates (it rotates instantly to desired side) trigger don't return block that is selected after rotating, so the player needs to rotate somewhere where blocks don't exist, and then return to desired block. I tried to do this with Arrays, and it was good but arrays don't have Clear method,so i can't work with them. I also used List, but same problem existed. Here is the picture: alt text

As you can see, it works, but if player rotates to block on the left side of the player (for us, it's block above player), it won't be selected:

alt text (Arrow is the direction of the player)

Code for trigger:

 using UnityEngine;
 using System.Collections;
 
 
 public class BlocksController : MonoBehaviour {
 
     
 
     //Cooldowns for minning
     private float nextAttack;
     public float coolDown;
 
     //Selected object
     Collider2D selectedBlock;
     bool isBlockSelected;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Space) && nextAttack <= Time.time)
         {
             if (isBlockSelected)
             {
                 selectedBlock.SendMessageUpwards("Mine", 1);
                 nextAttack = Time.time + coolDown;
                 Debug.Log("Minning!");
 
             }
             else print("No blocks selected!");
         }
         if(Input.GetKey(KeyCode.Space) && nextAttack <= Time.time)
         {
             if (isBlockSelected)
             {
                 selectedBlock.SendMessageUpwards("Mine", 1);
                 nextAttack = Time.time + coolDown;
                 Debug.Log("Minning!");
             }
             else print("No block selected!");
         }
         if (Input.GetKeyDown(KeyCode.KeypadEnter))
         {
             if (isBlockSelected) print("Only one block! " + selectedBlock.name);
             else print("No blocks selected!");
         }
     }
 
 
     void OnTriggerExit2D(Collider2D other)
     { 
         other.GetComponent<SpriteRenderer>().material.SetColor("_Color", Color.white);
         isBlockSelected = false;
         selectedBlock = null;        
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (!isBlockSelected)
         {
             selectedBlock = other;
             isBlockSelected = true;
             other.GetComponent<SpriteRenderer>().material.SetColor("_Color", Color.red);
         }
     }
 }

P.S. - English is not my native language, so sorry for any mistakes.

pic1.png (27.0 kB)
pic2.png (27.1 kB)
Comment
Add comment · Show 1
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 Pajgla · Oct 24, 2016 at 06:03 PM 0
Share

What could possibly be the problem : Variable 'isBlockSelected' don't change from true to false fast enough, so when other block is selected, if statement inside OnTriggerEnter is false. Any ideas ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Oct 24, 2016 at 08:14 PM

the new enter seems to be called before the exit of the old block. this would only happen if you moved left far enough to exit and then up because every orthogonal movement to the entered object won't exit.
I can think of two alternatives here:
-you're grid based so if you know which sell you're in, you know at which cell you're looking at. you don't even need physics for this indexed approach - if you must you could use a raycast, because it will always only hit one object at a time. on object change you can call your own exit on the old and enter on the new target

Comment
Add comment · Show 1 · 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 Pajgla · Oct 24, 2016 at 08:33 PM 0
Share

Yeah, i totally need new access to this. I will try with tile based system. Thank you for your reply anyway.

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

98 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

Related Questions

Audio Trigger 2D in C#? 1 Answer

Having trouble teleporting player in a 2D game 0 Answers

OnTriggerEnter2D() function not working 1 Answer

Spawning with trigger 2 Answers

How to identify which collider generated OnTriggerEnter2D 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