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 danysurvey93 · Feb 21, 2019 at 02:20 PM · 2dlist2d-physicscollision2dontriggerstay

How can i detect how many objects of the same type i'm triggering and choose only one of them?

Hi, i'm italian and my english is not good so... sorry! I'm developing a 2D game. In this game i can control a lumberman, so I can cut trees and move the wood wherever I want. if I do not have wood on me and the trigger detect the wood on floor, when i press "E", he takes the wood. The problem comes when i'm triggering 2 pieces of wood! In this case he collect both of them but, of course, then i have only 1 wood on the lumberman. I need to write a code that recognizes when I'm touching more than one object of the same type and, by pressing the "E" key, collect only one of these. Can someone help me? Thank you

Comment
Add comment · Show 4
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 dan_wipf · Feb 21, 2019 at 02:39 PM 0
Share

do you have some triggerzone? or how can you get in the colliders area? i’d recomend you to use a raycast and detect the gameobjects

avatar image zereda-games · Feb 21, 2019 at 02:56 PM 0
Share

Not this exactly But Something kinda along these lines:

  using UnityEngine;
  using System.Collections.Generic;
  public class HoriDoor$$anonymous$$anager : $$anonymous$$onoBehaviour
  {
      public DoorHori door1;
      public DoorHori door2;
      public bool isDoorClosed;
      const int DoorCount$$anonymous$$ey=12;
      public List<HoriDoor$$anonymous$$anager>$$anonymous$$anagerList=new  List<HoriDoor$$anonymous$$anager>(DoorCount$$anonymous$$ey);
     
      public HoriDoor$$anonymous$$anager(){

      }

      public HoriDoor$$anonymous$$anager(DoorHori d1,DoorHori d2){
         HoriDoor$$anonymous$$anager new$$anonymous$$anger = new HoriDoor$$anonymous$$anager();
         new$$anonymous$$anger.door1=d1;
         new$$anonymous$$anger.door2=d2;
         new$$anonymous$$anger.isDoorClosed=true;
         $$anonymous$$anagerList.Add(new$$anonymous$$anger);
      }

      public HoriDoor$$anonymous$$anager(DoorHori d1,DoorHori d2,bool locked){
         HoriDoor$$anonymous$$anager new$$anonymous$$anger = new HoriDoor$$anonymous$$anager();
         new$$anonymous$$anger.door1=d1;
         new$$anonymous$$anger.door2=d2;
         new$$anonymous$$anger.isDoorClosed=true;
         $$anonymous$$anagerList.Add(new$$anonymous$$anger);
      }

      void OnTriggerEnter()
      {
          if (isDoorClosed == false)
          {
              if (door1 != null)
              {
                  door1.OpenDoor();
              }
   
              if (door2 != null)
              {
                  door2.OpenDoor();
              }
          }
      }
   }
avatar image zereda-games zereda-games · Feb 21, 2019 at 02:58 PM 0
Share
  using UnityEngine;
  using System.Collections;
   
  public class DoorHori : $$anonymous$$onoBehaviour {
   
      public float translateValue;
      public float easeTime;
      public OTween.EaseType ease;
      public float waitTime;
   
      private Vector3 StartlocalPos;
      private Vector3 endlocalPos;
   
      private void Start(){
          StartlocalPos = transform.localPosition;
          gameObject.isStatic = false;
      }
       
      public void OpenDoor(){
          OTween.ValueTo( gameObject,ease,0.0f,-translateValue,easeTime,0.0f,"StartOpen","UpdateOpenDoor","EndOpen");
          GetComponent<AudioSource>().Play();
      }
   
      private void UpdateOpenDoor(float f){    
          Vector3 pos = transform.TransformDirection( new Vector3( 1,0,0));
          transform.localPosition = StartlocalPos + pos*f;
       
      }
   
      private void UpdateCloseDoor(float f){    
          Vector3 pos = transform.TransformDirection( new Vector3( -f,0,0)) ;
       
          transform.localPosition = endlocalPos-pos;
       
      }
   
      private void EndOpen(){
          endlocalPos = transform.localPosition ;
          StartCoroutine( WaitToClose());
      }
   
      private IEnumerator WaitToClose(){
       
          yield return new WaitForSeconds(waitTime);
          OTween.ValueTo( gameObject,ease,0.0f,translateValue,easeTime,0.0f,"StartClose","UpdateCloseDoor","EndClose");
          GetComponent<AudioSource>().Play();
      }
  }
avatar image zereda-games zereda-games · Feb 21, 2019 at 02:58 PM 0
Share
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
   
  public class DoorsLock$$anonymous$$anager : $$anonymous$$onoBehaviour
  {
      public bool lockStateRealTime = false;
      public Renderer rend;
      //Realized by looking at it that each manager deals with 1 door and has 2 for: A = unlocked, B = locked.
      public List<HoriDoor$$anonymous$$anager> door$$anonymous$$anagers=new List<HoriDoor$$anonymous$$anager>();
      private Shader unlitcolor;
      private List<GameObject> doorPlanes=new List<GameObject> ();
   
      private void Start()
      {
          doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
          ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
      }
   
      private void Change$$anonymous$$aterialSettings()
      {
          unlitcolor = Shader.Find("Unlit/Color");
          rend.material.shader = unlitcolor;
          rend.material.SetFloat("_$$anonymous$$etallic", 1);
      }
   
      private void ChangeColors(Color32 lockedColor, Color32 unlockedColor)
      {
          for (int i = 0; i < doorPlanes.Length; i++)
          {
              rend = doorPlanes[i].GetComponent<Renderer>();
              Change$$anonymous$$aterialSettings();
              if (door$$anonymous$$anager.isDoorClosed == true)
              {
                  rend.material.color = lockedColor;
              }
              else
              {
                  rend.material.color = unlockedColor;
              }
          }
      }
   
      // Update is called once per frame
      void Update()
      {
          lockStateRealTime = door$$anonymous$$anager.isDoorClosed;
          if (lockStateRealTime)
          {
              ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
              
          }
      }
  }


Also some of this is wrong... currently in the process of helpin someone

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Feb 21, 2019 at 02:53 PM

depends on how you plan to do it. Unity triggers events are only triggered by 2 colliders, so even if your lumberman is triggering multiple colliders, you wont receive one trigger message with all the colliders, but multiple trigger messages (one per collider in trigger). i think you dont really need to know when you are touching more than 1 object, since you can just pick up the first object and set a bool variable to hasTakenWood = true. and only take the wood if that var is set to false. if for some reason you need to know how many woods you are colliding with use the ontriggerstay for adding each collider of type wood he is getting trigger with and add it to a list. and in the fixedupdate clear that list.

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 zereda-games · Feb 21, 2019 at 03:03 PM 0
Share

I started realizing that also aver a suggestive comment, unless I $$anonymous$$now an answer for certain, not to post as an answer. Comment, and let others set it as an Answer, also try to reply to the specific comment in particular. Just thought I'd say so nicely ;)

avatar image zereda-games zereda-games · Feb 21, 2019 at 03:05 PM 0
Share

I asked why they didn't set their Clearly an Answer as a comment, and he said "well unless i get an upvote of a confirm i don't know it it is the answer to the question." I was like to myself Ahhhhh makes soo much more sense now.....

avatar image xxmariofer zereda-games · Feb 21, 2019 at 03:26 PM 0
Share

i know that both of this methods would work for his issues thats why i post it as an answer, i think i answered his topic, he explained his issue, and asked for a solution, i only told him he could do it other way and told him how to do it in the way he was asking. if you think my solution isnt clear/good please give it a down vote, since is how the forum works, and when the answer is correct give it an upvote. for basic questions like triggers i dont usually post them as a comment because i know it works, but if you think is not a valid answer please down vote it.

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

203 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 avatar image avatar image

Related Questions

How to make a little wiggle room for the collision? 1 Answer

2D Tilemaps: how to set colliders on scriptable tiles derived from Tilebase class 1 Answer

A node in a childnode? 1 Answer

How can I allow the rope to correctly interact with Box and Polygon colliders? 0 Answers

Different platform effector 2d rotations for different colliders 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