Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Manual

Interpolate#

Math / Interpolate Directions

  • Can Read

  • Cannot Write

Description#

Interpolates rotating two direction vectors from A to B given a ratio number

Code#

using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]
    
    [Title("Interpolate")]
    [Category("Math/Interpolate", "Directions")]
    
    [Description("Interpolates rotating two direction vectors from A to B given a ratio number")]
    [Image(typeof(IconInterpolate), ColorTheme.Type.Yellow, typeof(BadgeArrowRight))]
    
    [Keywords("Lerp", "Slerp", "Linear", "Smooth", "Damp", "Step", "Rotate")]
    
    [Serializable]
    public class TypeVector2MathInterpolateDirections : TypeVector2
    {
        [SerializeField] private GetVector2 m_DirectionA = new GetVector2();
        [SerializeField] private GetVector2 m_DirectionB = new GetVector2();
        
        [SerializeField] private GetNumber m_Ratio = new GetNumber();
        [SerializeField] private EaseMode m_Easing = EaseMode.Linear;
        [SerializeField] private GetBool m_Clamp = new GetBool();

        public override Vector2 GetPreview(GameObject source)
        {
            float ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.GetPreview(source));
            return Interpolate(this.m_DirectionA.GetPreview(source), this.m_DirectionB.GetPreview(source), ratio,
                this.m_Clamp.GetPreview(source));
        }

        public override Vector2 Get(Args args)
        {
            Vector2 a = this.m_DirectionA.Get(args);
            Vector2 b = this.m_DirectionB.Get(args);
            
            double ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.Get(args));
            bool clamp = this.m_Clamp.Get(args);
            
            return Interpolate(a, b, ratio, clamp);
        }

        private static Vector2 Interpolate(Vector2 a, Vector2 b, double ratio, bool clamp)
        {
            Quaternion rotationA = Quaternion.LookRotation(a, Vector3.forward).normalized;
            Quaternion rotationB = Quaternion.LookRotation(b, Vector3.forward).normalized;
            
            Quaternion interpolation = clamp
                ? Quaternion.Slerp(rotationA, rotationB, (float) ratio)
                : Quaternion.SlerpUnclamped(rotationA, rotationB, (float) ratio);
            
            float magnitude = Mathf.Lerp(a.magnitude, b.magnitude, (float) ratio);
            return interpolation * Vector2.right * magnitude;
        }
        
        public override string ToString()
        {
            return $"Interpolate {this.m_DirectionA} to {this.m_DirectionB} by {this.m_Ratio}";
        }
        
        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.