Forum rules - please read before posting.

Custom Action - Running twice

edited September 2019 in Technical Q&A

Unity Ver: 2019.2.4f1
AC Ver: 1.69.0

Hey folks,

I've made a custom action, but when using it in an action list it seems to trigger twice.

Here's the action list which is being run. As you can see the custom action just appears once:

And here's the code for the custom action:

namespace AC
{
    [System.Serializable]
    public class ActionThrowObjectAtPlayer : Action
    {
        // Declare variables here
        public GameObject objectToThrow;
        public GameObject spawnPoint;
        GameObject spawnedObject;
        Rigidbody rigidBody;
        public float throwForce;
        Vector3 dir;

        public ActionThrowObjectAtPlayer()
        {
            this.isDisplayed = true;
            category = ActionCategory.Custom;
            title = "Throw Object At Player";
            description = "Throws an object at the player";
        }

        override public float Run ()
        {

            Debug.Log("Run");
            /* 
             * This function is called when the action is performed.
             * 
             * The float to return is the time that the game
             * should wait before moving on to the next action.
             * Return 0f to make the action instantenous.
             * 
             * For actions that take longer than one frame,
             * you can return "defaultPauseTime" to make the game
             * re-run this function a short time later. You can
             * use the isRunning boolean to check if the action is
             * being run for the first time, eg: 
             */

            dir = AC.KickStarter.player.transform.position - spawnPoint.transform.position;
            dir = dir.normalized;
            dir = dir + new Vector3(0, 0.6f, 0);

            spawnedObject = Instantiate(objectToThrow, spawnPoint.transform.position, Quaternion.identity);
            Debug.Log(spawnedObject);

            rigidBody = spawnedObject.GetComponent<Rigidbody>();
            rigidBody.AddForce(dir * throwForce);

            if (!isRunning)
            {
                isRunning = true;
                return defaultPauseTime;
            }
            else
            {
                isRunning = false;
                return 0f;
            }
        }


        override public void Skip ()
        {
            /*
             * This function is called when the Action is skipped, as a
             * result of the player invoking the "EndCutscene" input.
             * 
             * It should perform the instructions of the Action instantly -
             * regardless of whether or not the Action itself has been run
             * normally yet.  If this method is left blank, then skipping
             * the Action will have no effect.  If this method is removed,
             * or if the Run() method call is left below, then skipping the
             * Action will cause it to run itself as normal.
             */

             Run ();
        }


        #if UNITY_EDITOR

        override public void ShowGUI ()
        {
            // Action-specific Inspector GUI code here

            spawnPoint = (GameObject)EditorGUILayout.ObjectField("Spawn point:", spawnPoint, typeof(GameObject), true);
            objectToThrow = (GameObject)EditorGUILayout.ObjectField("Object to throw:", objectToThrow, typeof(GameObject), true);
            throwForce = EditorGUILayout.FloatField("Throw force:", throwForce);

            AfterRunningOption ();
        }

        public override string SetLabel ()
        {
            // (Optional) Return a string used to describe the specific action's job.

            return string.Empty;
        }
        #endif  
    }
}

What I'm seeing in my game is that everything in Run() is being triggered twice. So with the above logs inside the code, this is what I'm seeing in the Unity console;

Any thoughts?

Comments

  • I also see the same results if I add logs to other custom actions.

  • edited September 2019

    Ah. It's all to do with return 0f;

    Yep. In my case this is a oneshot action. Only needs to happen once and what I hadn't clocked / understood in the template was the if(!isRunning) structure for things running more than a single frame.

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.
Do NOT follow this link or you will be banned from the site!