找回密码
 立即注册
楼主: 强子

[脚本] Unity3D多个动画同时存在时间的控制代码

[复制链接]
发表于 2013-3-7 13:13 | 显示全部楼层 |阅读模式
  1. var player_:GameObject;
  2. var run_Speed_:float=3.0;
  3. var rotate_Speed_:float=3.0;
  4. var time_:float;
  5. var jump_Speed:float=8.0f;
  6. var jump_Bool:boolean=true;
  7. //---------DaZhaoController
  8. var dazhao_Time:float;
  9. var dazhao_t_Bool:boolean=false;
  10. var dazhao_All_Controlelr:boolean=true;
  11. //---------

  12. //________________________________________________________move jump
  13. //var jumpSpeed : float = 4.0;
  14. //var gravity : float = 10.0;
  15. //______________________________________________________
  16. var cc_ : CharacterController ;
  17. var animation_:GameObject;
  18. //------j k key controller
  19. var jk_Time:float;
  20. var jk_t_Bool:boolean=false;
  21. var jk_All_Controller:boolean=true;
  22. //----------

  23. //------Jump controller
  24. var Jump_Time:float;
  25. var Jump_t_Bool:boolean=false;
  26. var Jump_All_Controller:boolean=true;
  27. //----------


  28. //跳跃的相对控制
  29. private var moveDirection : Vector3 = Vector3.zero;

  30. function OnGUI() {
  31.   
  32. }
  33. //--------------_____________________________________

  34. function Start()
  35. {
  36.         player_=GameObject.Find("Male_A1");
  37.                 animation_=GameObject.Find("Bip01");
  38.         var run_=player_.animation["Run"];
  39.   
  40.         run_.wrapMode=WrapMode.Loop;
  41.                 player_.animation["ShenLanYao"].wrapMode=WrapMode.Loop;
  42.                 player_.animation["FuXiongYangWang"].wrapMode=WrapMode.PingPong;
  43.         //        player_.animation["YueShuangTui"].wrapMode=WrapMode.Once;
  44.                 player_.animation["QianBuCeTi"].wrapMode=WrapMode.Once;
  45.                 player_.animation["DaZhao"].wrapMode=WrapMode.Once;
  46.                 player_.animation["ZhongQuan"].wrapMode=WrapMode.Once;
  47.                 player_.animation["YouTuiHuiTi"].wrapMode=WrapMode.Once;
  48.                 //space
  49.                 player_.animation["YueShuangTi"].wrapMode=WrapMode.Default;
  50.                 player_.animation["GaoKongLuoXia"].wrapMode=WrapMode.Once;
  51.                 //BaoZha_=GameObject.Find("BaoZ");
  52.                 //BaoZha_.SetActiveRecursively(false);
  53. }

  54. function Update () {

  55.          cc_ = GetComponent(CharacterController);
  56.                
  57.                 //-jump-------------------------
  58.                
  59.    /*if (cc_.isGrounded) {
  60.          // We are grounded, so recalculate
  61.          // move direction directly from axes
  62.          //我们着地了,所以直接通过轴重新计算move direction。
  63.          moveDirection = Vector3(0, 0,0);
  64.          moveDirection = transform.TransformDirection(moveDirection);
  65.          moveDirection *= 8.0;     

  66.      if (Input.GetKeyDown(KeyCode.Space) ) {
  67.                         moveDirection.y = 5;
  68.                         player_.animation.CrossFade("YueShuangTi");
  69.                 }
  70.    }
  71.    // Apply gravity
  72.    //应用重力。
  73.    moveDirection.y -= 8 * Time.deltaTime;
  74.    // Move the controller
  75.    //移动控制器。
  76.    cc_.Move(moveDirection * Time.deltaTime);*/
  77.                 //-jump-------------------------
  78.                
  79.                
  80.                
  81.                
  82.                
  83.                                 player_.animation["QianBuCeTi"].layer=1;
  84.                                 player_.animation["ZhongQuan"].layer=1;
  85.                                 player_.animation["DaZhao"].layer=1;
  86.                                 player_.animation["YueShuangTi"].layer=2;
  87.                                 //player_.animation["FuXiongYangWang"].layer=1;
  88.                                 //player_.animation["Run"].layer=1;
  89.                                 //player_.animation["DaZhao2"].layer=1;
  90.                                 //--全部设置为固定-攻击动作
  91.                         /*if(!player_.animation.IsPlaying("QianBuCeTi"))
  92.                         {
  93.                                 if(!player_.animation.IsPlaying("ZhongQuan"))
  94.                                 {
  95.                                         if(!player_.animation.IsPlaying("DaZhao"))
  96.                                         {
  97.                                                 
  98.                                         }
  99.                                 }
  100.                         }*/
  101.                         if(dazhao_All_Controlelr)//dazhao controller
  102.                         {
  103.                                 if(!player_.animation.IsPlaying("QianBuCeTi"))
  104.                                 {
  105.                                         if(!player_.animation.IsPlaying("ZhongQuan"))
  106.                                         {
  107.                                                         //--------------------------------------jump controller------------------------------------------
  108.                                                         if(Jump_All_Controller==true)
  109.                                                         {
  110.                                                                         if(cc_.isGrounded)
  111.                                                                         {
  112.                                                                                 //moveDirection=cc_.transform.position;
  113.                                                                                 //moveDirection*=8.0;
  114.                                                                                 if(Input.GetKeyDown(KeyCode.Space))
  115.                                                                                 {
  116.                                                                                         Jump_t_Bool=true;
  117.                                                                                         moveDirection.y=8;
  118.                                                                                         player_.animation.CrossFade("YueShuangTi");
  119.                                                                                         //cc_.transform.position.y=Mathf.Lerp(cc_.transform.position.y,cc_.transform.position.y+10,0.05);
  120.                                                                                        
  121.                                                                                         print("sddasd");
  122.                                                                                 }
  123.                                                                         }
  124.                                                                         
  125.                                                                
  126.                                                         }
  127.                                                         moveDirection.y-=20*Time.deltaTime;
  128.                                                         cc_.Move(moveDirection*Time.deltaTime);
  129.                                                         //--------------------------------jump time controller------
  130.                                                         if(Jump_t_Bool)
  131.                                                         {
  132.                                                                 Jump_Time+=Time.deltaTime;
  133.                                                                 if(Jump_Time<1.4)
  134.                                                                 {
  135.                                                                         Jump_All_Controller=false;
  136.                                                                 }
  137.                                                                 else if(Jump_Time>1.4)
  138.                                                                 {
  139.                                                                         Jump_t_Bool=false;
  140.                                                                         Jump_All_Controller=true;
  141.                                                                 }
  142.                                                                 //print(Jump_Time);
  143.                                                         }
  144.                                                         else if(Jump_t_Bool==false)
  145.                                                         {
  146.                                                                 Jump_Time=0;
  147.                                                         }
  148.                                                                 //--------------------------------jump time controller------
  149.                                                 //--------------------------------------jump controller------------------------------------------
  150.                                                         if(Input.GetKey(KeyCode.W))
  151.                                                    {
  152.                                                                 cc_.SimpleMove(cc_.transform.TransformDirection(Vector3.forward)*3);
  153.                                                                 //print(cc_.transform.TransformDirection(Vector3.forward)*3);
  154.                                                                
  155.                                                    }
  156.                                                    if(Input.GetKey(KeyCode.S))
  157.                                                    {
  158.                                                                 cc_.transform.Rotate(0,Mathf.Lerp(0,-360,Time.deltaTime),0);
  159.                                                                 cc_.SimpleMove(cc_.transform.TransformDirection(Vector3.forward)*-3);
  160.                                                                
  161.                                                    }
  162.                                                    if(Input.GetKey(KeyCode.A))
  163.                                                    {
  164.                                                                 cc_.transform.Rotate(0,-1,0);
  165.                                                    }
  166.                                                    if(Input.GetKey(KeyCode.D))
  167.                                                    {        
  168.                                                                 cc_.transform.Rotate(0,1,0);
  169.                                                    }
  170.                                                    if(Input.GetKeyDown(KeyCode.W))
  171.                                                    {
  172.                                                                 player_.audio.volume =Main_Window.scrollSetSound;
  173.                                                                 player_.audio.Play();
  174.                                                    }
  175.                                                    
  176.                                                         if(Mathf.Abs(Input.GetAxis("Vertical"))>0.1||Mathf.Abs(Input.GetAxis("Horizontal")))
  177.                                                         {
  178.                                                                 player_.animation.CrossFade("Run");
  179.                                                         //        print("da");
  180.                                                         
  181.                                                         }
  182.                                                         
  183.                                                         else
  184.                                                         {
  185.                                                                
  186.                                                                         player_.animation.CrossFade("FuXiongYangWang",0.5);
  187.                                                                
  188.                                                                
  189.                                                         }
  190.                                                 }
  191.                                 }
  192.                                        
  193.                                         //----------------------------------------Acttack controller-----------------------------------------------
  194.                                         if(jk_All_Controller)
  195.                                         {
  196.                                                 if(!player_.animation.IsPlaying("YueShuangTi"))
  197.                                                 {
  198.                                                         if(Input.GetKeyDown(KeyCode.K))
  199.                                                         {
  200.                                                                 jk_t_Bool=true;
  201.                                                                 player_.animation.CrossFade("QianBuCeTi");
  202.                                                                                 
  203.                                                                                 
  204.                                                                                 //BaoZha_.SetActiveRecursively(true);
  205.                                                                                 //Instantiate(BaoZha_,BaoZha_.transform.position,BaoZha_.transform.rotation);
  206.                                                         }
  207.                                                         if(Input.GetKeyDown(KeyCode.J))
  208.                                                         {
  209.                                                                 jk_t_Bool=true;
  210.                                                                 player_.animation.CrossFade("ZhongQuan");
  211.                                                                                 
  212.                                                                                 
  213.                                                                
  214.                                                                 }
  215.                                                 }
  216.                                        
  217.                                         }
  218.                                                                
  219.                                         if(jk_t_Bool)
  220.                                         {
  221.                                                         jk_Time+=Time.deltaTime;
  222.                                                         if(jk_Time<0.4)
  223.                                                         {
  224.                                                                 jk_All_Controller=false;
  225.                                                         }
  226.                                                         else if(jk_Time>0.5)
  227.                                                         {
  228.                                                                 jk_t_Bool=false;
  229.                                                                 jk_All_Controller=true;
  230.                                                         }
  231.                                         }
  232.                                         else if(jk_t_Bool==false)
  233.                                         {
  234.                                                         jk_Time=0;
  235.                                         }
  236.                                         //----acttack controller
  237.                                         //----------------------------------------Acttack controller-----------------------------------------------
  238.                                        
  239.                         }
  240.                         
  241.                         
  242.              //  cc_.transform.Rotate(0,Input.GetAxis("Horizontal")*rotate_Speed_,0);
  243.                          //--------------------------------------------------dazhao   controller------------------------------------------
  244.                          if(!player_.animation.IsPlaying("YueShuangTi"))
  245.                          {
  246.                                 if(Input.GetKeyDown(KeyCode.J)&&Input.GetKeyDown(KeyCode.K))
  247.                                 {
  248.                                         if(Power.powerValue>33) //judge  powerValue
  249.                                         {
  250.                                                 player_.animation.CrossFade("DaZhao");
  251.                                                 dazhao_t_Bool=true;//dazhao js controller
  252.                                                 dazhao_All_Controlelr=false;
  253.                                                 //cc_.Move(Vector3(0,1*Time.deltaTime,0));
  254.                                         }
  255.                                 }
  256.                          }
  257.                                 
  258.                                 if(dazhao_t_Bool)
  259.                                 {
  260.                                         dazhao_Time+=Time.deltaTime;
  261.                                         if(0.5<dazhao_Time&&dazhao_Time<1.2)//go
  262.                                         {
  263.                                                 cc_.SimpleMove(cc_.transform.TransformDirection(Vector3.forward)*6);
  264.                                         }
  265.                                         //dazhao_Bool=true;
  266.                                         else        if(dazhao_Time>4.8&&dazhao_Time<5.2)//back
  267.                                         {
  268.                                                 cc_.SimpleMove(cc_.transform.TransformDirection(Vector3.forward)*-12);
  269.                                         }
  270.                                         else        if(dazhao_Time>5.2)//false
  271.                                         {
  272.                                                 dazhao_t_Bool=false;
  273.                                                 dazhao_All_Controlelr=true;
  274.                                         }
  275.                                 }
  276.                                 else if(dazhao_t_Bool==false)
  277.                                 {
  278.                                         dazhao_Time=0;
  279.                                 }
  280.                         //--------------------------------------------------dazhao   controller------------------------------------------
  281. }

  282. function Do()
  283. {
  284.          yield WaitForSeconds (5.0);
  285. }
复制代码
发表于 2013-6-3 14:09 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2014-11-1 21:40 | 显示全部楼层
谢谢分享!
发表于 2017-3-2 07:00 | 显示全部楼层
楼主是超人
发表于 2017-3-2 07:42 | 显示全部楼层
好帖就是要顶
发表于 2017-3-2 07:05 | 显示全部楼层
难得一见的好帖
发表于 2017-3-2 07:21 | 显示全部楼层
很好哦
发表于 2017-3-2 07:29 | 显示全部楼层
不错不错
发表于 2017-5-12 15:06 | 显示全部楼层
楼主是超人
发表于 2017-5-12 15:14 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-30 11:58 , Processed in 0.095238 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表