Calculate the coordinates of any point between two points [Unity Mathematics]

Today, I will share the method of calculating the coordinates of any point between two points, which is divided into the calculation of 3d and 2d points. The idea is probably the coordinates of the starting point plus the offset relative to the starting point calculated according to the ratio

    /// <summary>
    /// 获取两点之间的点
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <param name="progress"></param>
    /// <returns></returns>
    public static Vector3 BetweenPoint(Vector3 start, Vector3 end, float progress)
    {
        Vector3 normal = (end - start).normalized;
        float distance = Vector3.Distance(start, end);
        return normal * (distance * progress) + start;
    }

    /// <summary>
    /// 获取两点之间的点
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <param name="progress"></param>
    /// <returns></returns>
    public static Vector2 BetweenPoint(Vector2 start, Vector2 end, float progress)
    {
        Vector2 normal = (end - start).normalized;
        float distance = Vector2.Distance(start, end);
        return normal * (distance * progress) + start;
    }

Guess you like

Origin blog.csdn.net/weixin_36719607/article/details/120412961