• 【自走棋】曲线箭头


    <1>效果:

    <2>思路:

    使用贝塞尔曲线和LineRender

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO.Compression;
    
    public class CurveTest : MonoBehaviour
    {
    
        public float height = 4;
        [SerializeField]
        private Vector3 P0;
        [SerializeField]
        private Vector3 P1;
        [SerializeField]
        private Vector3 P2 = Vector3.zero;
        [SerializeField]
        private LineRenderer lineRenderer;
    
        private float useTime = 0;
        private float interval = 0;
        private int pointNum = 0;
        private List<Vector3> lst = new List<Vector3>();
    
        private GameObject start;
        private GameObject end;
    
        private void reset()
        {
            useTime = 0;
            float val = (P2 - P0).magnitude;
            pointNum = Mathf.FloorToInt(val)+1;
            interval = 1.0f / pointNum;
            lst.Clear();
            Vector3 p = (P0 + P2) * 0.5f;
            P1 = p + Vector3.up* pointNum/2;
        }
    
        private void showLine()
        {
            //从0开始 num+1
            Vector3 pos0 = getPosByTime(useTime);
            lst.Add(pos0);
            for (int i = 1; i <= pointNum; i++)
            {
                useTime += interval;
                Vector3 pos = getPosByTime(useTime);
                lst.Add(pos);
            }
            //策略:根据P0-P2的距离 计算应该有多少个间隔
            lineRenderer.SetVertexCount(lst.Count);
            for (int i = 0; i < lst.Count; i++)
            {
                lineRenderer.SetPosition(i, lst[i]);
            }
            lineRenderer.material.SetTextureScale("_MainTex", new Vector2(  (int)(lst.Count * 0.9), 1));
            setStartEnd();
        }
    
        private void setStartEnd()
        {
            if (start == null) start = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            start.transform.localScale = Vector3.one * 0.3f;
            if (end == null) end = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            end.transform.localScale = Vector3.one * 0.3f;
            start.transform.position = P0;
            end.transform.position = P2;
        }
    
        /// <summary>
        /// 通过时间点获取时间
        /// </summary>
        /// <param name="useTime"></param>
        /// <returns></returns>
        private Vector3 getPosByTime(float useTime)
        {
            return (1 - useTime) * (1 - useTime) * P0 + 2 * useTime * (1 - useTime) * P1 + useTime * useTime * P2;
        }
    
        void OnGUI()
        {
            if (GUILayout.Button("重置", GUILayout.Width(100), GUILayout.Height(100)))
            {
                reset();
                showLine();
            }
        }
    
    }
    

      

  • 相关阅读:
    Codeforces 1485C Floor and Mod (枚举)
    CodeForces 1195D Submarine in the Rybinsk Sea (算贡献)
    CodeForces 1195C Basketball Exercise (线性DP)
    2021年初寒假训练第24场 B. 庆功会(搜索)
    任务分配(dp)
    开发工具的异常现象
    Telink MESH SDK 如何使用PWM
    Telink BLE MESH PWM波的小结
    [LeetCode] 1586. Binary Search Tree Iterator II
    [LeetCode] 1288. Remove Covered Intervals
  • 原文地址:https://www.cnblogs.com/cocotang/p/10825417.html
Copyright © 2020-2023  润新知