//

 //SpringCollider for unity-chan!

 //

 //Original Script is here:

 //ricopin / SpringCollider.cs

 //Rocket Jump : http://rocketjump.skr.jp/unity3d/109/

 //https://twitter.com/ricopin416

 //

 using UnityEngine;

 using System.Collections;

 namespace UnityChan

 {

     public class SpringCollider : MonoBehaviour

     {

         //半径

         public float radius = 0.5f;

         private void OnDrawGizmosSelected ()

         {

             Gizmos.color = Color.green;

             Gizmos.DrawWireSphere (transform.position, radius);

         }

     }

 }

SpringCollider

 //

 //SpringBone.cs for unity-chan!

 //

 //Original Script is here:

 //ricopin / SpringBone.cs

 //Rocket Jump : http://rocketjump.skr.jp/unity3d/109/

 //https://twitter.com/ricopin416

 //

 //Revised by N.Kobayashi 2014/06/20

 //

 using UnityEngine;

 using System.Collections;

 namespace UnityChan

 {

     public class SpringBone : MonoBehaviour

     {

         //次のボーン

         public Transform child;

         //ボーンの向き

         public Vector3 boneAxis = new Vector3 (-1.0f, 0.0f, 0.0f);

         public float radius = 0.05f;

         //各SpringBoneに設定されているstiffnessForceとdragForceを使用するか?

         public bool isUseEachBoneForceSettings = false; 

         //バネが戻る力

         public float stiffnessForce = 0.01f;

         //力の減衰力

         public float dragForce = 0.4f;

         public Vector3 springForce = new Vector3 (0.0f, -0.0001f, 0.0f);

         public SpringCollider[] colliders;

         public bool debug = true;

         //Kobayashi:Thredshold Starting to activate activeRatio

         public float threshold = 0.01f;

         private float springLength;

         private Quaternion localRotation;

         private Transform trs;

         private Vector3 currTipPos;

         private Vector3 prevTipPos;

         //Kobayashi

         private Transform org;

         //Kobayashi:Reference for "SpringManager" component with unitychan 

         private SpringManager managerRef;

         private void Awake ()

         {

             trs = transform;

             localRotation = transform.localRotation;

             //Kobayashi:Reference for "SpringManager" component with unitychan

             // GameObject.Find("unitychan_dynamic").GetComponent<SpringManager>();

             managerRef = GetParentSpringManager (transform);

         }

         private SpringManager GetParentSpringManager (Transform t)

         {

             var springManager = t.GetComponent<SpringManager> ();

             if (springManager != null)

                 return springManager;

             if (t.parent != null) {

                 return GetParentSpringManager (t.parent);

             }

             return null;

         }

         private void Start ()

         {

             springLength = Vector3.Distance (trs.position, child.position);

             currTipPos = child.position;

             prevTipPos = child.position;

         }

         public void UpdateSpring ()

         {

             //Kobayashi

             org = trs;

             //回転をリセット

             trs.localRotation = Quaternion.identity * localRotation;

             float sqrDt = Time.deltaTime * Time.deltaTime;

             //stiffness

             Vector3 force = trs.rotation * (boneAxis * stiffnessForce) / sqrDt;

             //drag

             force += (prevTipPos - currTipPos) * dragForce / sqrDt;

             force += springForce / sqrDt;

             //前フレームと値が同じにならないように

             Vector3 temp = currTipPos;

             //verlet

             currTipPos = (currTipPos - prevTipPos) + currTipPos + (force * sqrDt);

             //長さを元に戻す

             currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;

             //衝突判定

             for (int i = ; i < colliders.Length; i++) {

                 if (Vector3.Distance (currTipPos, colliders [i].transform.position) <= (radius + colliders [i].radius)) {

                     Vector3 normal = (currTipPos - colliders [i].transform.position).normalized;

                     currTipPos = colliders [i].transform.position + (normal * (radius + colliders [i].radius));

                     currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;

                 }

             }

             prevTipPos = temp;

             //回転を適用;

             Vector3 aimVector = trs.TransformDirection (boneAxis);

             Quaternion aimRotation = Quaternion.FromToRotation (aimVector, currTipPos - trs.position);

             //original

             //trs.rotation = aimRotation * trs.rotation;

             //Kobayahsi:Lerp with mixWeight

             Quaternion secondaryRotation = aimRotation * trs.rotation;

             trs.rotation = Quaternion.Lerp (org.rotation, secondaryRotation, managerRef.dynamicRatio);

         }

         private void OnDrawGizmos ()

         {

             if (debug) {

                 Gizmos.color = Color.yellow;

                 Gizmos.DrawWireSphere (currTipPos, radius);

             }

         }

     }

 }

SpringBone.cs

 //

 //SpingManager.cs for unity-chan!

 //

 //Original Script is here:

 //ricopin / SpingManager.cs

 //Rocket Jump : http://rocketjump.skr.jp/unity3d/109/

 //https://twitter.com/ricopin416

 //

 //Revised by N.Kobayashi 2014/06/24

 //           Y.Ebata

 //

 using UnityEngine;

 using System.Collections;

 namespace UnityChan

 {

     public class SpringManager : MonoBehaviour

     {

         //Kobayashi

         // DynamicRatio is paramater for activated level of dynamic animation 

         public float dynamicRatio = 1.0f;

         //Ebata

         public float            stiffnessForce;

         public AnimationCurve    stiffnessCurve;

         public float            dragForce;

         public AnimationCurve    dragCurve;

         public SpringBone[] springBones;

         void Start ()

         {

             UpdateParameters ();

         }

         void Update ()

         {

 #if UNITY_EDITOR

         //Kobayashi

         if(dynamicRatio >= 1.0f)

             dynamicRatio = 1.0f;

         else if(dynamicRatio <= 0.0f)

             dynamicRatio = 0.0f;

         //Ebata

         UpdateParameters();

 #endif

         }

         private void LateUpdate ()

         {

             //Kobayashi

             if (dynamicRatio != 0.0f) {

                 for (int i = ; i < springBones.Length; i++) {

                     if (dynamicRatio > springBones [i].threshold) {

                         springBones [i].UpdateSpring ();

                     }

                 }

             }

         }

         private void UpdateParameters ()

         {

             UpdateParameter ("stiffnessForce", stiffnessForce, stiffnessCurve);

             UpdateParameter ("dragForce", dragForce, dragCurve);

         }

         private void UpdateParameter (string fieldName, float baseValue, AnimationCurve curve)

         {

             var start = curve.keys [].time;

             var end = curve.keys [curve.length - ].time;

             //var step    = (end - start) / (springBones.Length - 1);

             var prop = springBones [].GetType ().GetField (fieldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

             for (int i = ; i < springBones.Length; i++) {

                 //Kobayashi

                 if (!springBones [i].isUseEachBoneForceSettings) {

                     var scale = curve.Evaluate (start + (end - start) * i / (springBones.Length - ));

                     prop.SetValue (springBones [i], baseValue * scale);

                 }

             }

         }

     }

 }

SpingManager.cs

unitychan-crs 头发随动脚本的更多相关文章

  1. Unity 头发随动效果

    目标 实现角色的衣袖.头发.裙摆.披风.尾巴等,在角色运动时,可以产生随动的效果.类似王者荣耀角色展示界面. 准备 源码出出处:https://github.com/unity3d-jp/unityc ...

  2. linux-启动脚本-souce与sh

    source:        在当前shell程序中执行,  因此当前shell程序中的变量和环境变量,均可见.   执行的脚本,能更新到当前shell程序. sh:            开启一个新 ...

  3. Seaweedfs-启动脚本

    #!/bin/bash if [ ! -e /sunlight/shell/main.sh ];then echo " [ Error ] file /sunlight/shell/main ...

  4. Tomcat8-启动脚本分析

    1. Tomcat也是一个java程序 最终的入口启动文件:org.apache.catalina.startup.Bootstrap 最后一条命令: start "Tomcat" ...

  5. NGUI学习笔记(五):缓动

    在Unity3D中可以使用自带的Animation制作任意形式的动画,不过我们这篇笔记主要是学习和使用NGUI提供的Tween动画.NGUI提供的Tween库功能较为简单,主要是用来实现NGUI自身需 ...

  6. Linux 设备模型浅析之 uevent 篇(2)

    Linux 设备模型浅析之 uevent 篇 本文属本人原创,欢迎转载,转载请注明出处.由于个人的见识和能力有限,不可能面 面俱到,也可能存在谬误,敬请网友指出,本人的邮箱是 yzq.seen@gma ...

  7. 关于Redis的知识汇总[转]

    1. Overview 1.1 资料 <The Little Redis Book> ,最好的入门小册子,可以先于一切文档之前看,免费. 作者Antirez的博客,Antirez维护的Re ...

  8. android udev

    http://www.freesoftwaremagazine.com/articles/drivers_linux http://blog.csdn.net/jianchi88/article/de ...

  9. mysql报错排查总结

    mysql报错: [root@zabbix ~]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through sock ...

随机推荐

  1. springboot-controller的使用

    获取url中的数据: @RestController public class HelloController { @RequestMapping(value="/say/{id}" ...

  2. Swing 添加超链接 打开页面

    http://lazycat774880994.iteye.com/blog/567412  Swing中打开一个连接或者web页面的一些记录,这几种方式是在项目中有这样子用到过,特来记录一下,以便下 ...

  3. [APIO2013]机器人

    题目描述 VRI(Voltron 机器人学会)的工程师建造了 n 个机器人.任意两个兼容的机 器人站在同一个格子时可以合并为一个复合机器人. 我们把机器人用 1 至 n 编号(n ≤ 9).如果两个机 ...

  4. INSPIRED启示录 读书笔记 - 第22章 原型测试

    物色测试者 1.如果你已经拥有一批特约用户,可以邀请他们参加测试 2.如果是企业级产品,同类产品的展销会是寻找目标用户的好去处 3.可以在分类信息网站上发布广告,征集测试者.征集要求可以写得笼统些,不 ...

  5. INSPIRED启示录 读书笔记 - 第19章 用户体验设计与实现

    先定义用户体验再动手开发 在软件开发过程中,有很多工作可以同时进行.比如,需求调研和产品设计(用户体验设计).开发与测试 尽管如此,用户体验设计和软件开发就不能同时进行,原因有五点 1.与软件开发团队 ...

  6. java基础之bit、byte、char、String

    bit 位,二进制数据0或1 byte 字节,一个字节等于8位二进制数 char 字符, String 字符串,一串字符 常见转换 1 字母  = 1byte = 8 bit 1 汉字  = 2byt ...

  7. 微信小程序与微信公众号同一用户登录问题

    微信小程序与微信公众号同一用户登录问题 最近在做微信小程序与微信公众号登录合并的接口.整理相关资料以及个人认识的心得写了这篇文章与大家一起分享. 首先,简单说下我遇到的问题是我们的程序调用微信小程序得 ...

  8. hql join

    文章一: 1.用hql语句 ` String hql="select student.id, student.name ,class.name from student映射实体类名 as s ...

  9. JDK各个版本的新特性jdk1.5-jdk8[转]

    JDK各个版本的新特性 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升级的新特性,这样才能循序渐进的学好一门语言.今天先为大家介绍一 ...

  10. 【P2401】不等数列(DP)

    这个题乍一看就应该是DP,再看一眼数据范围,1000..那就应该是了.然后就向DP的方向想,经过对小数据的计算可以得出,如果我们用f[i][j]来表示前i个数有j个是填了"<" ...