【教程】unity2d物理绳子效果

视频教程连接:https://www.bilibili.com/video/BV1as4y137Zd/?vd_source=e78cc1987a8d805f7d0980b75c086d02

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MyCord : MonoBehaviour
{

    [Header("p1的水平偏移")]
    public float P1HDrift;
    [Header("p2的水平偏移")]
    public float P2HDrift;
    [Header("p2的垂直偏移")]
    public float P2VDrift;
    [Header("每段曲线上的点数量")]
    public float LinePosCount;

    /// <summary>
    /// 链条上的点
    /// </summary>
    private List<Transform> _hingePosList;

    /// <summary>
    /// 线条渲染组件
    /// </summary>
    private LineRenderer _lr;


    // Start is called before the first frame update
    void Start()
    {
        _hingePosList = transform.GetComponentsInChildren<Transform>().ToList();
        _hingePosList.RemoveAt(0);

        _lr = GetComponent<LineRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        DrawLine();
    }

    /// <summary>
    /// 获取画线所需点
    /// </summary>
    /// <returns></returns>
    private List<Vector3> GetLinePosList()
    {

        //曲线上的点
        List<Vector3> linePosList = new List<Vector3>();

        //链条上的点
        List<Vector3> chainPosList = _hingePosList.Select(x => x.position).ToList();

        //前一个控制点
        Vector3? preP2 = null;

        //循环线段生成画线所需点

        for (int i = 0; i < chainPosList.Count - 1; i++)
        {

            //相邻三个点形成的夹角
            float angle = 0;
            //夹角影响系数
            float angleEffect = 0;

            if (i < chainPosList.Count - 2)
            {
                angle = Vector2.Angle(chainPosList[i] - chainPosList[i + 1], chainPosList[i + 2] - chainPosList[i + 1]);
                angleEffect = 1 - (angle / 180f);
            }

            //获取控制点p1  p2
            var ctrlPos = GetCtrlPoint(chainPosList[i], chainPosList[i + 1], angleEffect, preP2);
            preP2 = ctrlPos[1];

            //通过贝塞尔帮助类获取曲线点
            for (int j = 0; j < LinePosCount; j++)
            {
                Vector3 pos = Bezier(chainPosList[i], ctrlPos[0], ctrlPos[1], chainPosList[i + 1], j / (float)LinePosCount);
                linePosList.Add(pos);
            }
        }
        return linePosList;

    }

    /// <summary>
    /// 画线
    /// </summary>
    private void DrawLine()
    {
        var linePos = GetLinePosList();
        _lr.positionCount = linePos.Count;
        _lr.SetPositions(linePos.ToArray());
    }


    //三阶贝塞尔曲线推到公式
    public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        Vector3 temp;
        Vector3 p0p1 = (1 - t) * p0 + t * p1;
        Vector3 p1p2 = (1 - t) * p1 + t * p2;
        Vector3 p2p3 = (1 - t) * p2 + t * p3;
        Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
        Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
        temp = (1 - t) * p0p1p2 + t * p1p2p3;
        return temp;
    }

    /// <summary>
    /// 获取两个控制点
    /// </summary>
    /// <param name="p0">开始点</param>
    /// <param name="p3">结束点</param>
    /// <param name="preP2">前一个控制点p2</param>
    /// <param name="angleEffect">相邻三个点所形成角度的影响系数</param>
    /// <returns></returns>
    private Vector3[] GetCtrlPoint(Vector3 p0, Vector3 p3,float angleEffect, Vector3? preP2 = null)
    {
        Vector3 p1, p2;

        //先找到p0 p3 连线上的点
        p1 = p0 + (p3 - p0) * P1HDrift;
        p2 = p0 + (p3 - p0) * P2HDrift;

        //如果前一个p2不为null,p1就在  preP2  p0 所在直线上
        if (preP2 != null)
            p1 = p0 + (p0 - (Vector3)preP2);

        //在p0p3的垂直方向上进行偏移,得到偏移后的p2
        p2 = (Vector2)p2 + Vector2.Perpendicular(p3 - p0).normalized * angleEffect * P2VDrift;


        return new Vector3[] { p1, p2 };
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值