迷雾系统-人物驱散迷雾

使用linerRender,将人物移动数据动态添加进去,同样是特殊层级让FogCamera渲染

EndCapVertices的数量越多,矩形就变为一个椭圆形的形状,更适合圆形视野探索

当拐点的两个点距离太近,LineRender会发生扭曲,解决方案是在拐点处额外新添几个点作为过渡

下面是我每次鼠标点击移动,就往LineRender里面多新填一行数据,预防拐点变形,我每次点击就新填几行无意义数据进去,超过50个,我就新生成LinerRender

csharp 复制代码
 if (Input.GetMouseButtonUp(0))
      {
         Move(Input.mousePosition);
      }
      
      if (_isCanMove)
      {
         transform.Translate(_dir.normalized*Time.deltaTime*_moveSpeed,Space.World);
         _addLineTimer += Time.deltaTime;
         if (_addLineTimer > 0.1f)
         {
            _addLineTimer = 0f;
            AddLineRender(transform.position);
         }
         
         if (Vector3.Distance(transform.position, _targetPos) < 0.5f)
         {
            transform.position = _targetPos;
            _isCanMove = false;
            AddLineRender(transform.position);
         }
      }

//上面是Update里面的操作
 private void AddLineRender(Vector3 pos)
   {
      _currentLineRenderer.positionCount = _linerIndex+1;
      _currentLineRenderer.SetPosition(_linerIndex,pos);
   }

//鼠标点击生成此方法
   public void Move(Vector2 targetPos)
   {
      if (_currentLineRenderer == null)
      {
         GameObject go = Instantiate(meshPrefab,meshPar.transform);
         go.transform.position = new Vector3(0f, 0f, 1f);
         _currentLineRenderer = go.GetComponent<LineRenderer>();
         _addLineTimer = 0.1f;
         _linerIndex = 0; 
         AddLineRender(transform.position);
      }

      if (_linerIndex > 50)
      {
         GameObject go = Instantiate(meshPrefab,meshPar.transform);
         go.transform.position = new Vector3(0f, 0f, 1f);
         _currentLineRenderer = go.GetComponent<LineRenderer>();
         _addLineTimer = 0.1f;
         _linerIndex = 0;
         AddLineRender(transform.position);
         _linerIndex += 1;
      }
      else
      {
         for (int i = 0; i < 3; i++)
         {
            AddLineRender(transform.position + new Vector3(0.01f*i, 0f, 0f));
            _linerIndex += 1;
         }
      }

//获取移动向量,物体往此方向移动
 Vector3 pos = cam.ScreenToWorldPoint(new Vector3(targetPos.x,targetPos.y,-10f));
      _targetPos = new Vector3(pos.x,pos.y,-9.5f);
      _dir = _targetPos - transform.position;
      _isCanMove = true;
相关推荐
Lost of 程序猿12 分钟前
.NET 10 船端单机部署与离线升级实战:从闪退事故到无感回滚
c#·asp.net·.net
淡海水1 小时前
11-04-Unity-Span-foreach-LINQ的分配与热路径边界
unity·c#·linq·foreach·span
唐青枫15 小时前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
fogota16 小时前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
灸木17 小时前
C# 多线程与异步
c#
鱼听禅21 小时前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
geovindu1 天前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
淡海水1 天前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota1 天前
C# 如何调用系统图标
c#·.net
△曉風殘月〆1 天前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr