Unity 通过点击屏幕控制人物移动

http://blog.csdn.net/qqmcy/article/details/9391283


本文代码例子: http://vdisk.weibo.com/s/KLdrg

首先,直接将角色控制器包中的模型资源拖拽如层次视图当中。



           在Project视图中鼠标右键选择Import  Package ->Script引入官方提供的脚本,这些脚本主要是应用于摄像机朝向的部分。首先在Hierarchy视图中选择摄像机组件,接着在导航栏菜单中选择Compont -> Camera-Control ->SmoothFollow脚本。实际意义是将跟随脚本绑定在摄像机之上,目的是主角移动后摄像机也能跟随主角一并移动。如下图所示,脚本绑定完毕后可在右侧监测面板视图中看到Smooth Follow脚本。Target 就是射向摄像机朝向的参照物,这里把主角对象挂了上去意思是摄像机永远跟随主角移动。




由于官方提供的脚本并不是特别的好,摄像机永远照射在主角的后面,以至于控制主角向后回头时也无法看到主角的面部。可以做如下修改。

[csharp]  view plain copy print ?
  1. /* 
  2. This camera smoothes out rotation around the y-axis and height. 
  3. Horizontal Distance to the target is always fixed. 
  4.  
  5. There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves. 
  6.  
  7. For every of those smoothed values we calculate the wanted value and the current value. 
  8. Then we smooth it using the Lerp function. 
  9. Then we apply the smoothed values to the transform's position. 
  10. */  
  11.   
  12. // The target we are following  
  13. var target : Transform;  
  14. // The distance in the x-z plane to the target  
  15. var distance = 10.0;  
  16. // the height we want the camera to be above the target  
  17. var height = 5.0;  
  18. // How much we   
  19. var heightDamping = 2.0;  
  20. var rotationDamping = 3.0;  
  21.   
  22. // Place the script in the Camera-Control group in the component menu  
  23. @script AddComponentMenu("Camera-Control/Smooth Follow")  
  24.   
  25.   
  26. function LateUpdate () {  
  27.     // Early out if we don't have a target  
  28.     if (!target)  
  29.         return;  
  30.       
  31.     // Calculate the current rotation angles  
  32.     var wantedRotationAngle = target.eulerAngles.y;  
  33.     var wantedHeight = target.position.y + height;  
  34.           
  35.     var currentRotationAngle = transform.eulerAngles.y;  
  36.     var currentHeight = transform.position.y;  
  37.       
  38.     // Damp the rotation around the y-axis  
  39.     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);  
  40.   
  41.     // Damp the height  
  42.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);  
  43.   
  44.     // Convert the angle into a rotation  
  45.     //下面这行为官方的代码,我们将其注释掉  
  46. //  var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);  
  47.     //只需要需改这样就好。  
  48.     var currentRotation = 1;  
  49.       
  50.       
  51.     // Set the position of the camera on the x-z plane to:  
  52.     // distance meters behind the target  
  53.     transform.position = target.position;  
  54.     transform.position -= currentRotation * Vector3.forward * distance;  
  55.   
  56.     // Set the height of the camera  
  57.     transform.position.y = currentHeight;  
  58.       
  59.     // Always look at the target  
  60.     transform.LookAt (target);  
  61. }  





下面是我们自己写的脚本,写完与Constructor(主人公)相关联就OK了。


[csharp]  view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Controller1 : MonoBehaviour   
  5. {  
  6.     //状态:站立  
  7.     private const int HERO_IDLE = 0;  
  8.     //行走  
  9.     private const int HERO_WALK = 1;  
  10.     //跑  
  11.     private const int HERO_RUN = 2;  
  12.     //人物当前的状态  
  13.     private int gameState = 0;  
  14.     //记录触摸点的3D坐标  
  15.     private Vector3 point;  
  16.       
  17.     private float time;  
  18.       
  19.     void Start()  
  20.     {  
  21.         //初始设置人物状态:站立  
  22.         SetGameState(HERO_IDLE);  
  23.     }  
  24.       
  25.       
  26.     void Update()  
  27.     {  
  28.         //按下鼠标左键  
  29.         if(Input.GetMouseButtonDown(0))  
  30.         {  
  31.             //从摄像机的原点向鼠标点击的对象身上发射一条射线  
  32.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
  33.             RaycastHit hit;  
  34.             //当射线碰到对象时  
  35.             if(Physics.Raycast(ray,out hit))  
  36.             {  
  37.                 //目前场景中只有地形  
  38.                 //其实应当判断一下当前射线碰撞到的对象是否为地形。  
  39.                 //得到在3D世界中点击的坐标  
  40.                 point = hit.point;  
  41.                 //设置主角面朝这个点击的坐标  
  42.                 //这里设定的方向是鼠标选择的目标点在游戏世界点中的3D坐标。为了避免主人公的x  
  43.                 //轴与Zzhou发生旋转(特殊情况)所以我们设定朝向的Y轴永远是主人公的y轴。  
  44.                 transform.LookAt(new Vector3(point.x,transform.position.y,point.z));  
  45.                 //用户是否连续点击按钮  
  46.                 if(Time.realtimeSinceStartup - time <= 0.2f)  
  47.                 {  
  48.                     //连续点击 进入奔跑状态  
  49.                     SetGameState(HERO_RUN);  
  50.                 }  
  51.                 else  
  52.                 {  
  53.                     //点击一次只进入走路状态  
  54.                     SetGameState(HERO_WALK);  
  55.                 }  
  56.                 //记录点击鼠标的时间  
  57.                 time = Time.realtimeSinceStartup;  
  58.             }  
  59.         }  
  60.     }  
  61.       
  62.     void FixedUpdate()  
  63.     {  
  64.         switch(gameState)  
  65.         {  
  66.         case HERO_IDLE:  
  67.             break;  
  68.         case HERO_WALK:  
  69.             //移动主角一次移动的长度为0.05  
  70.             Move(0.05f);  
  71.             break;  
  72.         case HERO_RUN:  
  73.             //跑步是移动长度为0.1f  
  74.             Move(0.1f);  
  75.             break;  
  76.               
  77.         }  
  78.     }  
  79.       
  80.     void SetGameState(int state)  
  81.     {  
  82.         //动画效果  
  83.         switch(state)  
  84.         {  
  85.               
  86.         case HERO_IDLE:  
  87.             point = transform.position;  
  88.             animation.Play("idle");  
  89.             break;  
  90.         case HERO_WALK:  
  91.             animation.Play("walk");  
  92.             break;  
  93.         case HERO_RUN:  
  94.             animation.Play("run");  
  95.             break;  
  96.               
  97.         }  
  98.         gameState = state;  
  99.     }  
  100.       
  101.     void Move(float speed)  
  102.     {  
  103.         //主角没到达目标点时,一直向该点移动  
  104.         //在这里判断主人公当前位置是否到达目标位置,然后取得两点坐标差的绝对值。未到达目的  
  105.           
  106.         if(Mathf.Abs(Vector3.Distance(point,transform.position)) >= 1.3f)  
  107.         {  
  108.             //得到角色控制器  
  109.             CharacterController controller = GetComponent<CharacterController>();  
  110.             //设置移动  
  111.             //第一个参数:两个坐标点的差,参数2:需要移动的距离  
  112.             Vector3 v = Vector3.ClampMagnitude(point - transform.position,speed);  
  113.             //控制移动  
  114.             controller.Move(v);  
  115.         }  
  116.           
  117.         else  
  118.         {  
  119.             //到达目的地,继续站立状态  
  120.             SetGameState(HERO_IDLE);  
  121.         }  
  122.     }  
  123.       
  124. }  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值