Forum rules - please read before posting.

How to play custom animation before player starts walking?

I have a scene where the player can interact with a tree, I then play a custom animation of him climbing it. Now as he's sitting up there he's able to interact with the screen as normal, but if the player clicks on the ground to walk somewhere, I want to play an animation where he climbs back down before starting to walk to the destination clicked. How can I do that?

Comments

  • edited September 2023

    You'd need to hook into the OnPointAndClick custom event - which fires when the Player point-and-clicks on the NavMesh - to back-up the navigation path, run a Cutscene that handles the animation, and then move the Player along the path.

    To prevent this from running each time you move somewhere, create a Local Bool variable that's True when the Player is in the tree, and check its value first.

    Something along these lines ought to do it:

    using UnityEngine;
    using AC;
    
    public class ClimbDownFirst : MonoBehaviour
    {
    
        public int isInTreeLocalVariableID = 0;
        public Cutscene climbDownCutscene;
        Vector3[] backupPoints;
    
        void OnEnable ()
        {
            EventManager.OnPointAndClick += OnPointAndClick;
            EventManager.OnEndActionList += OnEndActionList;
        }
    
        void OnDisable ()
        {
            EventManager.OnPointAndClick -= OnPointAndClick;
            EventManager.OnEndActionList -= OnEndActionList;
        }
    
        void OnPointAndClick (Vector3[] pointArray, bool run)
        {
            if (!LocalVariables.GetBooleanValue (isInTreeLocalVariableID)) return;
    
            backupPoints = new Vector3[pointArray.Length];
            for (int i=0; i<pointArray.Length; i++)
            {
                backupPoints[i] = pointArray[i];    
                pointArray[i] = KickStarter.player.transform.position;
            }
            climbDownCutscene.Interact ();
        }
    
        void OnEndActionList (ActionList actionList, ActionListAsset actionListAsset, bool isSkipping)
        {
            if (actionList == climbDownCutscene)
            {
                KickStarter.player.MoveAlongPoints (backupPoints, false);
            }
        }
    
    }
    
  • That makes a lot of sense. I looked in the manual and the tutorial you linked, but I don't understand where specifically to add the Event Manager script component as it needs to be running all the time from my understanding?

  • The above script file (ClimbDownFirst.cs) only needs to be added in the scene that you need it to work in. I'm presuming this is a specific situation for a single scene?

    The Event Manager component is separate - it's part of AC and you don't need to add it manually.

  • Yes it's a local single scene. I'm not a programmer, so please bare with me. :)

    I've created an empty gameobject and added the script to it.
    I've created a local bool variable called isInTreeLocalVariableID, it starts out False.
    When the player climbs the tree, this variable is set to True.
    I've created a cutscene called climbDownCutscene and it runs a check of the variable, if False do nothing, if True play climb down animation and set variable to False.

    It doesn't seem to work. It doesn't play the climbing down animation when clicking on the nav mesh, it just snap and walks. I'm not sure if the variable check is supposed to be on the climbDownCutscene or what I'm doing wrong?
    There are no issues in the console.

  • What are your AC/Unity versions?

    You won't need to check for the variable in your Cutscene - the script will handle that for you.

    You will, though, need to fill in the Inspector fields of the script's component. Make sure your climbDownCutscene is assigned in its "Climb Down Cutscene" field, and that the "Is In Tree Local Variable ID" field is set to the ID of the local bool variable you created.

    Is the Variable being set to False once clicking on the NavMesh while up in the Tree, which should only occur if the climbDownCutscene is being run?

  • edited September 2023

    I'm running Unity 2022.3.4f1 and AC 1.78.3
    I've tried adding a camera shake to the top of the cutscene actionlist just to be certain it isn't an animation problem. But the cutscene just isn't executed.

    This image shows the script component settings, the variable, the actionlist when climbing the tree, and the cutscene that doesn't play.

    https://imgbox.com/qoQoA68F

  • In the component's Inspector, set the "Is In Tree Local Variable ID" field to 2. This field refers to the ID number of the variable to check (to the left of its name in the Variables Manager), rather than the value itself.

  • That worked, thanks a lot Chris! Now I know what variable ID's are. :)

  • So I just updated to the 1.79.1 and I get a compiling error due to the script above, how can I fix that?

    Assets\HOLLOW\Scripts\ClimbDownFirst.cs(19,41): error CS0123: No overload for 'OnPointAndClick' matches delegate 'EventManager.Delegate_OnPointAndClick'

  • See the "Upgrade notes" section of the Changelog for v1.79.0:

    • The OnPointAndClick custom event now uses the "ref" keyword for its pointArray parameter

    To fiex, replace:

    void OnPointAndClick (Vector3[] pointArray, bool run)
    

    with:

    void OnPointAndClick (ref Vector3[] pointArray, bool run)
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.