Simple custom controllers

Fortunately, creating a custom controller can be quite simple! First, you should decide if you really need a custom controller. In particular, if you want offset your GameObject from the source, make your GameObject a child of the actual controller object and offset that!

The most easy way to add custom code to a controller is to inherit a new class from a controller (SplineController, PathController or VolumeController) and override at least one method:

UserAfterUpdate()

This method is called everytime after controller has updated. It's only called if the source is fully initialized and the controller is playing, so you don't need any validation checks in here.

UserAfterInit()

This method is called after the controller has initialized, you don't need any source validation checks in here!

Inside the editor this method is called after you change inspector values or stop the preview

ApplyTransformPosition()

This method is called whenever the controller needs to set the transform's position. By overriding it you can e.g. call Rigidbody.MovePosition instead.

ApplyTransformRotation()

This method is called whenever the controller needs to set the transform's rotation. By overriding it you can e.g. call Rigidbody.MoveRotation instead.

Example

This example (used by the 01_Metadata example scene) shows a simple custom controller:

public class MetaDataController : SplineController
    {
        //The section attribute renders our field inside it's own category!
        [Section("MetaController",Sort=0)]
        [RangeEx(0, 30)]
        [SerializeField]
        float m_MaxHeight = 5f; // The height over ground to use as default
        

        public float MaxHeight
        {
            get { return m_MaxHeight; }
            set
            {
                if (m_MaxHeight != value)
                    m_MaxHeight = value;
            }
        }

        /// <summary>
        /// This is called just after the SplineController has been initialized
        /// </summary>
        protected override void UserAfterInit()
        {
            setHeight();
        }
        /// <summary>
        /// This is called just after the SplineController updates
        /// </summary>
        protected override void UserAfterUpdate()
        {
            setHeight();
        }


        void setHeight()
        {
            // Get the interpolated Metadata value for the current 
            // position (for SplineController, RelativePosition means TF)
            // If values can't be interpolated (no next value), current value (if present)
            // or default type value (for float that's 0) is returned
            var v = Spline.InterpolateMetadata<HeightMetadata,float>(RelativePosition);
         
            // In our case we store a percentage (0..1) in our custom MetaData class,
            // so we multiply with MaxHeight to set the actual height.
            // Note that position and rotation  has been already set by the SplineController,
            // so we just translate here using the local y-axis
           
            transform.Translate(0, v * MaxHeight, 0, Space.Self);
           
        }


    }
}

Last modified: 2015/09/29 13:51 by Jake