• 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
Question by sukebei · Jul 24, 2021 at 03:02 PM · delegatelistenersequence

Using Listeners for Sequences and Different Classes

I have three classes of C# t$$anonymous$$ngs. One is a PuzzleController, another is a CameraGlide, and the last is a CameraRotate. Just a quick summary of what the 3 classes are. Controller has all the puzzle variables, trigger actions, stuff to send to UI, and controls what state the puzzle is in. CameraGlide moves the camera from one point to another w$$anonymous$$le looking at a point. CameraRotate allows the player to mouse drag the mouse around a point and zoom in/out.


What I want to do is t$$anonymous$$s:

-Player walks into trigger

-Camera disables 3rdPersonCamera

-Camera glides from be$$anonymous$$nd player to predetermined puzzle location

-Tells the Controller when the camera has reached the puzzle position

-PuzzleController turns off the glide and turns on the Rotate


-Player out of trigger

-Turn off CameraRotate

-CameraGlides from where it is to the player

-Tells the controller when the camera has reached the player position

-Turn back on 3rdPersonCamera


The problem is the sequence of events. CameraGlide and CameraRotate should be PuzzleController agnostic (there different classes of controllers at different levels). So I don't want to hard code anyt$$anonymous$$ng into Glide Update() somet$$anonymous$$ng like:

"if(at_loc) Puz_Con_A.glideDone()".

Because now t$$anonymous$$s Glide class only works with the one type of controller. Similarly I don't want a constantly running update in the PuzzleController checking the status of the Glide like:

"if(glide.at_loc =true)".

Since t$$anonymous$$s would be constantly checking when it only needs to be when the player has triggered the puzzle.

Ideally I want to use some kind of Delegate/Listener that any PuzzleController can add to their own code so that when it triggers it tells the CameraGlide, and listens for CameraGlide to send back that it's at the location. The problem is I'm terrible at Listeners and I'm not sure how to add it w$$anonymous$$le keeping CameraGlide and CameraRotate agnostic.

Comment

People who like this

0 Show 0
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

Answer by tyruji · Jul 24, 2021 at 03:13 PM

You definitely might want to check out UnityEvents, as of right now you are going in a direction in w$$anonymous$$ch every class will have to know about each other just to subscribe to some events, and by using UnityEvents they won't have to know about each other and your code can stay clean. e.g. you said there is a trigger so let's assume t$$anonymous$$s class:

 using UnityEngine;
 using UnityEngine.Events;  // important to use UnityEvents
 
 class Trigger : MonoBehaviour
 {
        // ...
        [SerializeField] private UnityEvent _OnPlayerEnter = null;
 
        private void OnTriggerEnter( Collision col )  // or whatever you want
        {
               Player p;
               // ... your logic for player checking or whatever ...
               _OnPlayerEnter?.Invoke(); // call the event - the player entered
        }
 }

Then you can subscribe various stuff to the event and have everyt$$anonymous$$ng smoothly organised. UnityEvents can handle:

  • No parameter public methods

  • Public methods with 1 parameter

  • Setting a public field to a given value

Using them is pretty intuitive, so I bet you'll get the hang of it. Good luck!

Comment

People who like this

0 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 sukebei · Jul 24, 2021 at 04:20 PM 0
Share

Okay but how do I add it to do things and wait?

So I make a UnityEvent _event in the PuzzleController right? Then when the puzzlecontroller gets the trigger I need to tell it to start the cameraGlide and then WAIT. When the cameraGlide is at the location, the puzzle controller needs to turn on the CameraRotate.

This is what I'm confused on, how is cameraGlide supposed to Invoke the next event? Because I can't Invoke cameraRotate until the glide has completed.

avatar image tyruji · Jul 24, 2021 at 04:31 PM 0
Share

You could make a public method in "cameraGlide" that starts it's logic and put another UnityEvent "_OnLocationReached" (or whatever you want to name it) in your "CameraGlide" class, and you could just drag in the CameraRotate to turn it on.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

UI toggle.onValueChanged assigning method via script 1 Answer

How to add a function from another class to a Slider's OnValueChanged with AddListener()? 1 Answer

Button Listener Takes Reference But I Want to Pass Value? 1 Answer

Sequence of methods stored in the list with parameters 1 Answer

Gesture Sequences in javascript 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