Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Official World Interaction System

Discussion in 'Open Projects' started by Amel-Unity, Dec 11, 2020.

  1. Amel-Unity

    Amel-Unity

    Unity Technologies

    Joined:
    Jan 30, 2020
    Posts:
    42
    Hello everyone :)

    This is a thread to discuss the world interaction system I have presented in the 5th episode.
    A system that allows to perform these possible interactions for now:
    • Pick up
    • Cook
    • Talk
    This system could be extended to later add more types of interactions in the game, if needed.

    Here is a screenshot of the diagram shown and explained during the episode:

    Interactiondiagram.png

    Card on the roadmap
     
  2. Sebastiran

    Sebastiran

    Joined:
    Feb 23, 2017
    Posts:
    5
    Great presentation! I think it's a great base, but I'm wondering if we've considered triggering actions mid-animation. Hitting an enemy for example would look great if it matches the movement of the attack, instead of triggering right at the start of the animation. The same thing would apply for picking items up etc.
    Some of these events like start cooking are great as it is because they wouldn't really require an animation. But things like Item Pickup (which communicate with the state machine) do. With the current state machine we are using there is no feature to trigger things mid-animation. There is only Start, End, and Update.
    There is also the trouble that logic and animation are done on two different state machine systems. The Unity Animator has excellent features that allow you to trigger events mid animation with Events and Curves in the Animation tab of the model. The Unity Animator also allows for adding of behaviour to each state, similar to what we are doing now in a completely different state machine. I'm not sure if this has been talking about before, either on the forums or on the live stream, or if this is a topic to discuss in this thread or to open up the Generic State Machine thread discussion again. Anyway, those are my thoughts. :)

    Edit: I just saw the roadmap has a card for Animation integration to wire behaviour with the animations that someone on the Unity team is working on. That might be what I was talking about?
     
    Last edited: Dec 14, 2020
  3. Amel-Unity

    Amel-Unity

    Unity Technologies

    Joined:
    Jan 30, 2020
    Posts:
    42
    Hi Sebastiran, that's a good point. For now I can think of 2 possible solutions:
    1. Using the state machine, we could create a new type of Action, which is played instantly (OnStateEnter) but its effects happen with a delay so it has a Coroutine inside it.
    2. We can use Animation events. Here we have 2 things to consider:
    • We will need to duplicate each animation we want to use mid-animation actions with as the animations are read only by default and we cannot add animation events to them before duplication.
    • For instance, for the pig chef, we will only be able to call functions that are on a script that is attached to the pig chef game object. So we will need to create a script that we can call ''AnimationEvents'' that will take care of doing the link between the animation event and the functions that need to be called from a child game object for instance. If I take the pick up as example, the interaction manager script that is taking care of it, is currently a child game object to the pig chef. So we need to use the ''AnimationEvents'' to reference it from the pig chef object and call the function for the Animation event.

    For both solutions, they need to be updated in case we update the animation(s), even though this will not likely happen or at least not so many times so it shouldn't be a problem.

    These are quick solutions I thought of but please feel free to propose any other solution(s) :)
     
  4. Amel-Unity

    Amel-Unity

    Unity Technologies

    Joined:
    Jan 30, 2020
    Posts:
    42
    Quick update: We merged Interaction branch (which also contains updates from Inventory) into main :)
     
    Sebastiran and treivize like this.
  5. Sebastiran

    Sebastiran

    Joined:
    Feb 23, 2017
    Posts:
    5
    I've thought about that first options too and I think it might be the logical choice as it will use the system we already have instead of implementing another system like an AnimationEvents script. Though would this method mean that every character would need their own scriptable objects for every animated event? For example, if a Critter has an attack event that is on a timer / Coroutine in an ActionSO, another Critter won't be able to use the same ActionSO right? As it would cause the previous timer to be overwritten. So we would have to detach the IEnumerator from the ActionSO and put it on a script that is on the Critter gameobject itself. But now that feels a bit like a loop in which it might have just been easier to use a script like AnimationEvents. Or is my thinking not correct?

    I'm not sure I understand the first consideration you mention. Is it not possible to add an animation event to the original, or do I understand that wrong?

    In my own game I'm using animation events for the Protagonist, as well as the enemies. The attack animation for example has two events: StartAttack and EndAttack. Every frame in between those two events is being checked for collision between the weapon (sphere raycasts at different positions along the weapon) and an enemy.

    Other animations like Running have two event calls to FootStep, which will play a sound when a foot lands on the ground. I'm not sure if that is something we would want in this game too, but I'm wondering if it would be wise to use ActionSO's and Coroutines for that as it is something that loops. And if there is even the slightest deviation in the animation timing and the time you gave the Coroutine, eventually they will not run in sync anymore.

    I would say both options work, but it is really dependent on what you would require the game to have. Or maybe someone else has another even better idea?
     
  6. treivize

    treivize

    Joined:
    Jan 27, 2016
    Posts:
    135
    Hi @Amel-Unity, do you know when the alignment between main and art-assets should happen?
     
  7. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    742
    No, the ScriptableObjects that make the Actions(SO) are yes, ScriptableObjects, but they "spin" an instance at runtime which is independent and per-object, so in your example for instance every Critter would have its own, independent timer. So no issue on that front.

    Yes, Animation Clips are non-editable files when they come as sub-assets of an .fbx (as is our case). To add an Animation event, you'd need to duplicate the clip in the .fbx to make it disconnected from it and thus, editable.
    We might end up doing it anyway, but it's always annoying as every time there is an animation change you need to go in, duplicate it again, and rebuild the events at the right time.
    And by the way, all of our animations will have revisions.

    No, totally. For those uses, Animation Events would be the best way to go because you'd be able to easily sync them to the action.

    --

    Personally, I'm in favour of using a special, delayed Action to perform the release of events that are delayed from the beginning of an animation state. So we avoid duplicating the Animation Clips, for now.

    And then later if we see that it's a good idea to use Animation Events because we use them anyway, we can move the code to be called by one of them.
     
    Sebastiran likes this.
  8. Sebastiran

    Sebastiran

    Joined:
    Feb 23, 2017
    Posts:
    5
    Thank you so much for taking the time to reply and explain also on the Generic State Machine thread. It's a lot more understandable now. Very cleverly done with the ActionSO's that create instances. And it's such a nice way to split up the behaviour into different parts and not ending up with a 500 lines of code PlayerBehaviour script. You've convinced me! Haha.

    As for the adding of animation events goes: You are able to add events and curves through the fbx file settings in the animations tab. This way you don't have to edit the animation file itself and there is no need to duplicate it (See the two screenshots). Or is there a downside to this I'm not aware of? Anyway, thanks again for explaining!
    upload_2020-12-18_10-29-6.png

    upload_2020-12-18_10-19-49.png
     
    cirocontinisio likes this.
  9. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    742
    No, you're totally right! I forgot about this thing. I don't know when it was introduced, but it was added after I used Animation events in a real project (2014...) so I tend to forget it exists *insert facepalm emoji*

    I don't know, what do other people think too? (screams into the void)
     
  10. treivize

    treivize

    Joined:
    Jan 27, 2016
    Posts:
    135
    @Amel-Unity, I am having a look at your interaction system to implement the attack interaction between the player and the critters

    I have couple of questions that I would like to clarify before going further:
    - Do we need a separate input for attack action? Why not re-using the one used for the current interactions (cook/pickup/talk)? Do you see a problem with that?
    - Is it expected to have an attack interaction manager/controller in addition to your new Interaction manager or it could be included as a new interaction type inside it?
     
  11. Amel-Unity

    Amel-Unity

    Unity Technologies

    Joined:
    Jan 30, 2020
    Posts:
    42
    We can use a different input for the attack. When I worked on the interaction system I preferred to not include the attack mainly because the attack can be performed at any point in the game. So when we press the attack button, it will for instance, perform the attack and play the animation regardless of whether we are close to critters or not.
    When it comes to interaction, not like the attack, we need to make sure that it is possible to perform an action so we only take into consideration the input when we are close to an interactable object.
    Regarding the use of another manager or controller, yes we can create a new one specific to the attack, apart from the interaction
     
    treivize likes this.
  12. treivize

    treivize

    Joined:
    Jan 27, 2016
    Posts:
    135
    Hi @Amel-Unity, thanks for the clarification!

    Ideally the combat system should be similar to the one in Zelda Ocarina of time (sorry I have this old reference :p), where the player can attack anywhere, but will hit the enemy only when the weapon will collide with the critter. Is it the right direction for this system? I guess it will deserve a dedicated thread since it is separated from the interaction system.
     
  13. Amel-Unity

    Amel-Unity

    Unity Technologies

    Joined:
    Jan 30, 2020
    Posts:
    42
    Yes the attack will damage the enemy only when the weapon collides with the critter and yes it's definitely better to start a new thread so I just created the combat system thread and added a new card for it on the roadmap :)
     
    treivize likes this.
  14. treivize

    treivize

    Joined:
    Jan 27, 2016
    Posts:
    135
    Hi @Amel-Unity,

    I have opened a PR proposing an enhancement to your interaction system. The aim is to support multiple interactions at the same time if encountered during the game, like multiple collectable items, without having to have to move the player so as to trigger a collider exit and re enter in the interaction manager.

    In addition, I have added a correction in the ResetInteraction() to avoid inhibiting interaction when a random collider is triggering an exit in the script (like the weapon during the walking animation).

    The PR is here: https://github.com/UnityTechnologies/open-project-1/pull/301
     
  15. deivsky

    deivsky

    Joined:
    Nov 3, 2019
    Posts:
    86
    Hey all! I'd like to bring up this issue here.

    It seems to me like the InteractionManager needs some polish, move some stuff over to the FSM, and reduce the number of things it's trying to accomplish.

    I kind of want to work on it but I don't think I'll have much time until the weekend, so feel free to give it a go. ;)
     
    cirocontinisio likes this.
  16. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    742
    Finally took the time to do a few changes in the InteractionManager script.
    I integrated AlexandreGheraibia's PR (which I think had the right idea to break the interaction in 2 phases: button press and effects of the action), but then I tweaked a lot of things. And removed, like, a million lines :D

    So now the interaction looks much more like the fighting system. There's an empty child object ("InteractionDetector") whose sole role is to detect if something interactable enters it. If so, it notifies the InteractionManager on the root. Also this collider trigger, like the ones on the enemies, uses the ZoneTriggerController script which then notifies the InteractionDetector.

    The reason I moved InteractionDetector on the root is because I needed to be able to call a function (Collect) to trigger the actual collection of an item mid-way through the animation. As you know, AnimationEvents can only communicate with the object that has the Animator component (= the root). But this is good, again, it's now more aligned with what we do for combat and in combat Animation Clips.

    For this reason I actually removed the Action script that AlexandreGherabia made, since the only moment the SM needs to notify the script is when collecting something, via the animation above. This is because the other world interaction actions (Talk and Cook) are instantaneous, so they can be verified straight away inside the InteractionManager script.

    (will push soon)
     
  17. alex311

    alex311

    Joined:
    Dec 20, 2012
    Posts:
    6
    It's smart to call the event in the middle of the animation but I didn't understand how you check the state to Talk and Cook.
    Because you need state machine feedback for them too. Otherwise we can always attack and interact with these interactions
     
    Last edited: Feb 7, 2021
  18. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    742
    Talk and Cook are special world interactions because they send you to a different gameplay state (respectively dialogue and menus), so they will need to be handled differently. Pickup is the only one which happens seamlessly during gameplay.

    But yes, we will need to take care of those too! Still wiring all the different systems together :)
     
  19. alex311

    alex311

    Joined:
    Dec 20, 2012
    Posts:
    6
    Ok, thanks for the clarification
     
    cirocontinisio likes this.
unityunity