Unity模拟龙之谷人物行走简单控制

我个人挺喜欢龙之谷(DN)的人物控制的(不是广告哈....),就是人物太萌了一点,动作、打击感都挺好的。

今天用Unity简单模仿了一下DN的人物控制,当然,游戏里面动作很多,我这里只做了简单的walk和run的测试哈,但是感觉也蛮舒服的,哈哈。

期待的效果:鼠标旋转控制视角位置,滚轮控制镜头缩放。点击一次W键为行走,快速点击两次为奔跑。

1.准给工作:

场景中,

一个Camera、一块地皮、一只Cube



2.镜头的缩放和旋转实现:

看下Camera的组件:


再看下Cube的组件:


mouselook和smoothfollow的脚本就不贴出来了,都有的。

为了方便,下面是NMove和TestMove的代码:

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class NMove : MonoBehaviour {  
  5.   
  6.     //注意,开始之所以在滑动滚轮的时候,相机是抛物线的形式靠近/远离目标的,原因是,目标模型的中心点设置在了脚底。  
  7.     //解决方法:设置初始时,相机的高度与人物模型中心的高度一致,即可!  
  8.     public int MouseWheelSensitivity = 5;   //鼠标敏感度  
  9.     public int MouseZoomMin = 2;            //最小值  
  10.     public int MouseZoomMax = 10;           //最大值  
  11.     public float normalDistance;            //正常距离  
  12.     public GameObject Camera;  
  13.     smooth_follow CameraScript;       
  14.     // Use this for initialization  
  15.     void Start ()   
  16.     {  
  17.         CameraScript = Camera.GetComponent<smooth_follow>();    
  18.     }         
  19.     void LateUpdate()  
  20.     {  
  21.         if (Input.GetAxis("Mouse ScrollWheel") != 0)        //转动了滚轮  
  22.         {  
  23.             Debug.Log(Input.GetAxis("Mouse ScrollWheel"));  
  24.             //Debug.Log(distance);  
  25.             if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)  
  26.             {  
  27.                 normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;  
  28.             }  
  29.             if (normalDistance < MouseZoomMin)  
  30.             {  
  31.                 normalDistance = MouseZoomMin;  
  32.             }  
  33.             if (normalDistance > MouseZoomMax)  
  34.             {  
  35.                 normalDistance = MouseZoomMax;  
  36.             }  
  37.             CameraScript.distance = normalDistance;  
  38.         }  
  39.     }  
  40. }  
TestMove.cs:
[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class TestMove : MonoBehaviour {  
  5.   
  6.     public float speed = 1.0f;  
  7.     public GameObject camera;  
  8.     //当按下行走坐标后,物体旋转至camera的方向  
  9.     public float rospeed = 1.0f;    //rotate speed  
  10.     // Update is called once per frame  
  11.     void Update () {  
  12.         if (Input.GetKey(KeyCode.W | KeyCode.S | KeyCode.A | KeyCode.D))  
  13.         {   
  14.             //按下了行走键,旋转              
  15.             transform.rotation = Quaternion.Slerp(transform.rotation,camera.transform.rotation ,Time.deltaTime*rospeed);          
  16.             transform.eulerAngles = new UnityEngine.Vector3(0,transform.eulerAngles.y,transform.eulerAngles.z);  
  17.             //notice only Quaternion have Slerp methods.  
  18.         }  
  19.         if (Input.GetKey(KeyCode.W))  
  20.         {  
  21.             this.transform.Translate(Vector3.forward*Time.deltaTime*speed);  
  22.         }  
  23.         if (Input.GetKey(KeyCode.S))  
  24.         {  
  25.             this.transform.Translate(Vector3.forward * Time.deltaTime * speed * -1);  
  26.         }  
  27.         if (Input.GetKey(KeyCode.D))  
  28.         {  
  29.             this.transform.Translate(Vector3.right * Time.deltaTime * speed);  
  30.         }  
  31.         if (Input.GetKey(KeyCode.A))  
  32.         {  
  33.             this.transform.Translate(Vector3.right * Time.deltaTime * speed * -1);  
  34.         }  
  35.   
  36.       
  37.     }  
  38. }  

OK,现在你的Cube角色已经活动自如了!

注意,我这里用Cube因为是标准的立方体,主要是为了方便。

3.添加你喜欢的人物模型,制作状态机,使Cube成为其父物体:



状态机:



下面是PlayerSM(state machine)的代码:

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. /// <summary>  
  5. /// @author ZJC  player state machine study note   
  6. /// 问题:  
  7. /// 1.如何实现对同一按键点击两次run,点击一次walk?设置一个信号signal进行区分.要注意按键的逻辑顺序问题,这是关键  
  8. /// 2.人物转身的时候,有点斜着飘  
  9. /// (恩,这个问题解决,就是girl的父物体,我用了一个标准的cube,这样cube旋转的时候,就不会有那种斜着旋转的效果了,girl也就不会了。  
  10. /// </summary>  
  11. public class PlayerSM : MonoBehaviour {  
  12.   
  13.     private Animator animator;      
  14.     // 动画状态机参数Key  
  15.     private static readonly string ActionCMD = "ActionCMD";  
  16.     private static readonly string Run = "run";     
  17.     float timefirst = 0f;   //记录按下W的时间  
  18.     float timesecond = 0f;  
  19.     int n = 0;  
  20.     bool runsignal = false;  
  21.     public float KeyTime = 0.3f;  
  22.     void Start()  
  23.     {  
  24.         animator = this.GetComponent<Animator>();     
  25.     }     
  26.     // Update is called once per frame  
  27.     void Update ()  
[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.     {  
  2.          
  3.         AnimatorStateInfo stateinfo = animator.GetCurrentAnimatorStateInfo(0);           
  4.         if (Input.GetKey(KeyCode.W))  
  5.         {  
  6.          
  7.             print("runsigl = "+runsignal);  
  8.             if ( !runsignal )   
  9.             {  
  10.                 animator.SetInteger(ActionCMD, 1);  
  11.          
  12.             }   
  13.             else  
  14.             {                  
  15.                 animator.SetInteger(Run, 1);  
  16.                 animator.SetInteger(ActionCMD, 0);  
  17.                 print("wwwww+runsigal = " + runsignal);  
  18.             }             
  19.         }      
  20.         if (Input.GetKeyDown("w") )   
  21.         {  
  22.           //  print("up.....");  
  23.            // runsignal = false;  
  24.           
  25.                 if (n == 0)  
  26.                 {  
  27.                     timefirst = Time.time;  
  28.                     n++;  
  29.                 }  
  30.                 else if (n == 1)  
  31.                 {  
  32.                     timesecond = Time.time;  
  33.                     n = 0;  
  34.                 }  
  35.                 if (Mathf.Abs(timesecond - timefirst) <= KeyTime)  
  36.                 {  
  37.                     // print("wreff");  
  38.                     animator.SetInteger(Run, 1);  
  39.                     print("run = 1..........................");  
  40.                     runsignal = true;  
  41.                 }              
  42.            
  43.         }  
  44.         if(!Input.anyKey)   //只要按下了键(包括持续按键),就为真,否则为false  
  45.         {  
  46.             //参数清0        
  47.             animator.SetInteger(ActionCMD, 0);  
  48.             animator.SetInteger(Run, 0);  
  49.             runsignal = false;           
  50.   
  51.         }  
  52.          
  53.     }  
  54. }  

4.隐藏掉cube的mesh render.再测试,OK!

效果:鼠标位置控制旋转视角、滚轮控制视角缩放、点击一次W人物行走,快速点击两次奔跑,无按键为idle状态。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值