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 /
  • Help Room /
avatar image
0
Question by inget-namn · Mar 27 at 06:56 PM · unity 2dinteractable

Check for inherited class in overlaped Collider2D and execute functions (I think?)

Working on making a character controller for a 2d side scroller, and I want to keep as many functionalities things as possible in it. Disclaimer - I am not very good at coding and mainly rely on brute force to make things work.

Currently trying to get the ability to interact working, but I'm getting stuck.

What I want to do: I made a class which all interactables inherits from. The player should be able to walk up to the interactable. From the array that I get from the OverlapCircleAll I wish to then grab the currentInteractable and perform the appropriate function.

Problem is: It ain't doing it... Seems that it isn't finding the currentInteractable, or it isn't able to execute the function. Anyone who's better at code that can help me out?

 //Interactable Script, in a separate script and inherits all functions from another class as you can see

 public class CubeInteractable : Interactable
 {
     public override void OnRange()
     {
         Debug.Log("Can Interact");
     }
 
     public override void OnInteract()
     {
         Debug.Log("Interacted");
     }
 
     public override void OnLoseRange()
     {
         Debug.Log("No Interact");
     }
 }
 
 Character Controller in a separate script
 
     [Header("Interaction")]
     public bool canInteract = true;
     [SerializeField] private float interactionRadius = 2;
     [SerializeField] public LayerMask whatIsInteractable = default; //=7
     private Interactable currentInteractable;
 
 private void Update()
     {
             if (canInteract)
                 HandleInteraction();
     {
 
 private void HandleInteraction()
     {
 
         Collider2D[] hitInteractable = Physics2D.OverlapCircleAll(transform.position, interactionRadius, whatIsInteractable);
 
         
         foreach (Collider2D interactable in hitInteractable)
         {
 
                 interactable.gameObject.TryGetComponent(out currentInteractable);
 
                 if (currentInteractable)
                 {
                     currentInteractable.OnRange();
                 }
                 else
                 {
                     currentInteractable.OnLoseRange();
                     currentInteractable = null;
                 }
         }
 
 
         if (Input.GetKeyDown(KeyCode.E))
         {
 
             foreach(Collider2D interactable in hitInteractable)
             {
                 if (currentInteractable != null)
                    currentInteractable.OnInteract();
                 
                 else
                    return;
             }
 
         }
     }
 





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
Best Answer

Answer by inget-namn · Mar 27 at 11:39 PM

First of all, I don't think I made any actual changes... But after a reboot of the computer the script worked.

Realized some other problems however - the OnRange(); would get called every update, and the OnLoseRange(); would not get called at all. Not what I intended. Took a walk with the dog and got back with a refreshed mind and figuered out the solution. If anyone is looking for a solution for 2D interaction, then maybe this will help! Didn't change the Interactable script, and below you can see the other changes made. Note, this is not the entire script.

     [Header("Interaction Parameters")]
     [SerializeField] private float interactionRadius = 2;
     [SerializeField] public LayerMask whatIsInteractable = default;
     private Interactable currentInteractable;
     private bool couldInteract = false;
     private bool isInteractable = false;
 public bool canInteract =true;
 
 private void Update()
 {
             if (canInteract)
                 HandleInteraction();
 }
 
 //interaction radius visualized
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(transform.position, interactionRadius);
     }
 
     private void HandleInteraction()
     {
         Collider2D[] hitInteractable = Physics2D.OverlapCircleAll(transform.position, interactionRadius, whatIsInteractable);
         isInteractable = Physics2D.OverlapCircle(transform.position, interactionRadius, whatIsInteractable);
 
         foreach (Collider2D interactable in hitInteractable)
         {
             if(currentInteractable == null)
                 couldInteract = true;
 
             if (isInteractable == true)
             {
                 interactable.gameObject.TryGetComponent(out currentInteractable);
 
                 if (currentInteractable && couldInteract == true)
                 {
                     currentInteractable.OnRange();
                     couldInteract = false;
                 }
             }
 
             if (Input.GetKeyDown(KeyCode.E) && currentInteractable != null)
             {
                 currentInteractable.OnInteract();
             }
         }
 
         if (currentInteractable && isInteractable == false)
         {
             currentInteractable.OnLoseRange();
             currentInteractable = null;
         }
 
     }

 
Comment
Add comment · 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

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

188 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

Related Questions

Unity WebGL video black screen appears in Safari 13 0 Answers

Player making "random jumps" while is moving at random places (Unity 2D) 0 Answers

Graphic Raycasting doesnt work on all children canvases 2 Answers

transform.LookAt on 2d 0 Answers

Dynamic Wave Manipulation 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