参考:https://github.com/dci05049/Verlet-Rope-Unity
使用插件:Dotween

绳子代码修改:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RopeBridge : MonoBehaviour
{public Transform StartPoint;public Transform EndPoint;private LineRenderer lineRenderer;public List<RopeSegment> ropeSegments = new List<RopeSegment>();public float ropeSegLen = 0.25f;public int segmentLength = 35;public float startWidth = 0.1f;public float endWidth = 0.1f;// Use this for initializationvoid Start(){this.lineRenderer = this.GetComponent<LineRenderer>();Vector3 ropeStartPoint = StartPoint.position;for (int i = 0; i < segmentLength; i++){this.ropeSegments.Add(new RopeSegment(ropeStartPoint));ropeStartPoint.y -= ropeSegLen;}}// Update is called once per framevoid Update(){this.DrawRope();}private void FixedUpdate(){this.Simulate();}private void Simulate(){// SIMULATIONVector2 forceGravity = new Vector2(0f, -1f);for (int i = 1; i < this.segmentLength; i++){RopeSegment firstSegment = this.ropeSegments[i];Vector2 velocity = firstSegment.posNow - firstSegment.posOld;firstSegment.posOld = firstSegment.posNow;firstSegment.posNow += velocity;firstSegment.posNow += forceGravity * Time.fixedDeltaTime;this.ropeSegments[i] = firstSegment;}//CONSTRAINTSfor (int i = 0; i < 50; i++){this.ApplyConstraint();}}private void ApplyConstraint(){//Constrant to First Point RopeSegment firstSegment = this.ropeSegments[0];firstSegment.posNow = this.StartPoint.position;this.ropeSegments[0] = firstSegment;//Constrant to Second Point RopeSegment endSegment = this.ropeSegments[this.ropeSegments.Count - 1];endSegment.posNow = this.EndPoint.position;this.ropeSegments[this.ropeSegments.Count - 1] = endSegment;for (int i = 0; i < this.segmentLength - 1; i++){RopeSegment firstSeg = this.ropeSegments[i];RopeSegment secondSeg = this.ropeSegments[i + 1];float dist = (firstSeg.posNow - secondSeg.posNow).magnitude;float error = Mathf.Abs(dist - this.ropeSegLen);Vector2 changeDir = Vector2.zero;if (dist > ropeSegLen){changeDir = (firstSeg.posNow - secondSeg.posNow).normalized;}else if (dist < ropeSegLen){changeDir = (secondSeg.posNow - firstSeg.posNow).normalized;}Vector2 changeAmount = changeDir * error;if (i != 0){firstSeg.posNow -= changeAmount * 0.5f;this.ropeSegments[i] = firstSeg;secondSeg.posNow += changeAmount * 0.5f;this.ropeSegments[i + 1] = secondSeg;}else{secondSeg.posNow += changeAmount;this.ropeSegments[i + 1] = secondSeg;}}}private void DrawRope(){lineRenderer.startWidth = this.startWidth;lineRenderer.endWidth = this.endWidth;Vector3[] ropePositions = new Vector3[this.segmentLength];for (int i = 0; i < this.segmentLength; i++){ropePositions[i] = this.ropeSegments[i].posNow;}lineRenderer.positionCount = ropePositions.Length;lineRenderer.SetPositions(ropePositions);}public struct RopeSegment{public Vector2 posNow;public Vector2 posOld;public RopeSegment(Vector2 pos){this.posNow = pos;this.posOld = pos;}}void OnDrawGizmos(){DrawBGLine();}private void DrawBGLine(){#if UNITY_EDITORGizmos.color = Color.green;Gizmos.DrawLine(StartPoint.position, EndPoint.position);#endif}
}

动画代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;
using UnityEngine.Events;public class RopeAnim:MonoBehaviour
{Transform parent;RopeBridge ropeBridge;Vector3 target;Tween tween;public float percent = 0.5f;public UnityEvent OnAnimEnd;public float duration=10;// Start is called before the first frame updatevoid Start(){ropeBridge = GetComponent<RopeBridge>();target = ropeBridge.EndPoint.position;ropeBridge.EndPoint.position = ropeBridge.StartPoint.position;parent = transform.parent;startWidth = ropeBridge.startWidth;endwidth = ropeBridge.endWidth;InitAnim();}float startWidth = 0;float endwidth = 0;public void InitAnim(){ropeBridge.EndPoint.position = ropeBridge.StartPoint.position;ropeBridge.startWidth = 0f;ropeBridge.endWidth = 0f;if (parent == null){for (int i = 0; i < transform.childCount; i++) {RopeAnim childAnim = transform.GetChild(i).GetComponent<RopeAnim>();if (childAnim != null) {childAnim.InitAnim();}}}}// Update is called once per framevoid Update(){}public void Play(){//ropeBridge.lineWidth = 0.1f;ropeBridge.startWidth = startWidth;ropeBridge.endWidth = endwidth;ropeBridge.ropeSegLen = 0.01f;tween =ropeBridge.EndPoint.DOMove(target, duration).OnComplete(OnComplete).OnUpdate(OnUpdate);}private void OnUpdate(){if (parent != null){if (parent.GetComponent<RopeBridge>()){RopeBridge parentBridge = parent.GetComponent<RopeBridge>();int NodePoint = (int)(parentBridge.segmentLength * percent);if (NodePoint > parentBridge.segmentLength - 1){NodePoint = parentBridge.segmentLength - 1;}if (NodePoint < 0){NodePoint = 0;}ropeBridge.StartPoint.position = parentBridge.ropeSegments[NodePoint].posNow;}}}private void OnComplete(){if (OnAnimEnd != null){OnAnimEnd.Invoke();}if (parent != null) {ropeBridge.ropeSegLen = 0.05f;ropeBridge.EndPoint.DOShakePosition(1, 0.2f, 5, 10).SetLoops(-1).OnUpdate(OnUpdate);}}public void Stop(){tween.Pause();}
}

Unity中作2D藤曼生长效果相关推荐

  1. Unity中的关节组件和绳子效果的实现

    前言 在Unity中可以使用Joint(关节)来实现绳子的效果. Joint属于Unity里面的一种物理组件,是模拟物体与物体之间的一种连接关系,有以下几种类型:Hinge Joint(链条连接),F ...

  2. 在unity中使用EasyAR插件实现AR效果

    1.在EasyAR官网注册账号:https://www.easyar.cn/,然后进入自己的账号,去获取一个密钥 2.填写一些必要信息,点击确认即可获取密钥,然后查看自己新建的密钥 查看密钥 注:如果 ...

  3. unity中的2D虚拟摇杆和3D虚拟摇杆

    源代码链接https://github.com/hiramtan/HiJoystick_unity 如何使用 可以从此链接下载最新的unity package: 完成功能 2D虚拟摇杆 3D虚拟摇杆 ...

  4. Unity中使用Post Processing 开自发光效果

    正常设置步骤 1.项目中导入Post Processing插件 2.在Camera上添加 Post-process Layer 组件,选择Trigger为当前相机,Layer选择PostProcess ...

  5. Unity中实现3D人物残影效果

    一:效果演示 二:思路 --使用SkinnedMeshRenderer类中的BakeMesh方法去复制出新的Mesh --再使用Graphics.DrawMesh绘制会Mesh 三:核心代码实现 -- ...

  6. 【100个 Unity实用技能】☀️ | Unity中自定义 2D Sprite 精灵图显示顺序

    Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 . 包括游戏开发.美术.建筑.汽车设计.影视在内的所有创作者,借助 Unity 将创意 ...

  7. 记录一个在Unity中实现生成无限向上旋转楼梯效果

    最近因为要做一个小游戏,要实现用代码创建无限向上延伸的旋转楼梯效果,找遍了网上的逻辑都未曾实现,于是乎自己实现了一个,这里记录一下.有需要的朋友拿走不谢- 效果图: 直接上代码: using Syst ...

  8. Unity中画2D图表(2)——用XChart包绘制散点分布图 + 一条直线方程

    散点图用于显示关系. 对于 [相关性] ,散点图有助于显示两个变量之间线性关系的强度. 对于 [回归] ,散点图常常会添加拟合线. 举例1:你可以展示[年降雨量]与[玉米亩产量]的关系 举例2:你也可 ...

  9. tilemap 导入unity_教程|Unity中使用Tilemap快速创建2D游戏世界

    Custom Tiles Palette包含瀑布瓦片.如果选中瀑布规则瓦片,也就是Waterfall-RuleTile,可以看到它的导出类型为动画(Animate).这样就可以为每个瓦片指定动画帧. ...

最新文章

  1. PHP的内存泄露问题与垃圾回收
  2. 『互联网架构』软件架构-spring源码之spring结构概述
  3. ai保存web格式没有html,存储技巧,讲解AI存储为WEB所用格式的一些知识
  4. 匿名提问:rm -rf了怎么办?
  5. ncurses鼠标事件:mousemask(),ALL_MOUSE_EVENTS,KEY_MOUSE,getmouse(),mouse_grafo(),wmouse_trafo()
  6. Python趣味编程3则:李白买酒、猴子吃桃、宝塔上的琉璃灯
  7. Web开发中的主要概念
  8. web服务器一些概念
  9. Redis之各版本特性
  10. bitmap存入mysql,[MySQL] mysql中bitmap的简单运用
  11. 如何创建一个简易的HTML网页框架
  12. Guawa的Splitter的工具类
  13. php写aaa-zzz,php算法打印aa aaa ab aab直到zzz
  14. pandas数据分组聚合——groupby()、aggregate()、apply()、transform()和filter()方法详解
  15. 新一代网状网协议T-Mesh无线通信技术优势介绍
  16. Summit Wireless全新低成本空间音频模块现已震撼上市
  17. matplotlib画多个子图
  18. 基于共聚焦显微技术的显微镜和荧光显微镜的区别
  19. 红米k30可以用鸿蒙系统吗,红米K30遭狙击:挖孔双摄+麒麟990+鸿蒙OS系统 华为5G强势来袭...
  20. Webpack项目中引入IconFont图标

热门文章

  1. mongodb索引---复合索引
  2. 求教Word自动生成的目录中标题无法顶格,编号与标题之间也存在空格无法消除怎么办,
  3. (理解)单链表算法循环条件while(p)和while(p->next)有什么区别
  4. 基于OMPL库的RRT*算法实现
  5. Linux-ubuntu 安装与常用软件的安装
  6. 微信删除朋友圈多久会从服务器消失,6.6亿人消失在朋友圈,微信的社交属性正在降低?你多久没发了?...
  7. launchctl 启动进程控制
  8. 【名场面临摹 之 马里奥·奥德赛】3.1 马里奥的闲置(Idle)动画
  9. 哈希hash(散列)表结构详解
  10. proxytable代理不生效_proxyTable代理跨域使用详解