External Motions

Creating new external motion modules

  1. Create a new script that will inherit from the ExternalMotionData class and define the name of the motion.

using System;

namespace UHFPS.Runtime
{
    [Serializable]
    public class TestExternalMotion : ExternalMotionData
    {
        public override string Name => "Test Motion";

        public override ExternalMotionModule GetPosition => null;

        public override ExternalMotionModule GetRotation => null;
    }
}
  1. Create a new structure that will contain variables for position and rotation settings.

using System;
using UnityEngine;

namespace UHFPS.Runtime
{
    [Serializable]
    public class TestExternalMotion : ExternalMotionData
    {
        [Serializable]
        public struct TestSettings
        {
            public Vector3 Force;
            public float Duration;
        }

        public TestSettings PositionSettings;
        public TestSettings RotationSettings;

        public override string Name => "Test Motion";

        public override ExternalMotionModule GetPosition => null;

        public override ExternalMotionModule GetRotation => null;
    }
}
  1. Create a new subclass inside the base external motion class that will inherit from the ExternalMotionModule class.

// ... public class TestExternalMotion : ExternalMotionData

public sealed class TestForce : ExternalMotionModule
{
    public override bool IsFinished => Time.time > endTime;

    private readonly Vector3 force;
    private readonly float endTime;

    public TestForce(TestSettings settings)
    {
        force = settings.Force;
        endTime = Time.time + settings.Duration;
    }

    public override Vector3 Evaluate()
    {
        return force;
    }
}

The Evaluate() method will be called every time until IsFinished becomes true. The return value of the Evaluate() method is the force that will be applied to camera motion.

  1. Assign a class that inherits from ExternalMotionModule to the GetPosition and GetRotation properties.

Here is the full code of the test force module:

using System;
using UnityEngine;

namespace UHFPS.Runtime
{
    [Serializable]
    public class TestExternalMotion : ExternalMotionData
    {
        [Serializable]
        public struct TestSettings
        {
            public Vector3 Force;
            public float Duration;
        }

        public TestSettings PositionSettings;
        public TestSettings RotationSettings;

        public override string Name => "Test Motion";

        public override ExternalMotionModule GetPosition => new TestForce(PositionSettings);

        public override ExternalMotionModule GetRotation => new TestForce(RotationSettings);

        public sealed class TestForce : ExternalMotionModule
        {
            public override bool IsFinished => Time.time > endTime;

            private readonly Vector3 force;
            private readonly float endTime;

            public TestForce(TestSettings settings)
            {
                force = settings.Force;
                endTime = Time.time + settings.Duration;
            }

            public override Vector3 Evaluate()
            {
                return force;
            }
        }
    }
}
  1. After creating a new external motion module, you will be able to add this module to the External Motion State Data of the Player Item.

Last updated