最新补充。
一般在做鼠标选择时是从摄像机向目标点发送一条射线,然后取得射线与对象相交的点来计算3D目标点。后来在开发中发现了一个问题(射线被别的对象挡住了),就是如果主角的前面有别的游戏对象挡着。此时如果使用射线的原理,鼠标选择被档的对象,这样主角就会向被当的对象的方向行走。为了解决这个问题,我放弃使用发送射线的方法,最后通过2D的方法完美的处理了这个问题。
如下图所示,我们先把主角的3D坐标换算成屏幕中的2D坐标,当鼠标在屏幕中点击的时候取得一个目标点的2D坐标,根据这2个坐标换算出主角的2D向量。
然后我们在看看代码。
//将世界坐标换算成屏幕坐标
Vector3 vpos3 = Camera.main.WorldToScreenPoint(transform.position);
Vector2 vpos2 = new Vector2 (vpos3.x,vpos3.y);
//取得鼠标点击的屏幕坐标
Vector2 input = new Vector2 (Input.mousePosition.x,Input.mousePosition.y);
//取得主角到目标点的向量
Vector2 normalied = ((vpos2 – input)).normalized;
注意normalized是格式化向量,以为vpos2 – input是计算两个向量之间的距离,格式化后才是它们的方向。格式化后向量的取值范围在 -1 到 +1 之间。
//我们忽略Y轴的向量,把2D向量应用在3D向量中。
Vecotr3 targetDirection = new Vector3(normalied.x,0.0f,normalied.y) ;
//根据照相机的角度计算真实的方向
float y = Camera.main.transform.rotation.eulerAngles.y;
targetDirection = Quaternion.Euler(0f,y – 180,0f) * targetDirection;
摄像机的角度决定着主角移动的方向,y是摄像机当前角度,180是摄像机默认的角度,摄像机在旋转的时候y是会动态改变的,所以需要 y – 180 。用Quaternion.Euler()方法计算一个rotation ,然后乘以默认的向量targetDirection就是主角在3D中真实需要移动的方向。
//最后使用角色控制器移动主角就可以
Vector3 movement = targetDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity;
CharacterController controller = GetComponent<CharacterController>();
collisionFlags = controller.Move(movement);
不知道大家理解了没有?如果没有理解就在我的博客下面留言,我回即时的解答的、OK继续忙碌拉。
详细代码示例请看这篇文章. Unity3D研究院之处理摄像机跟随避免相机穿墙拉近的方法(四十四)
———————————————————–华丽的分割线—————————————-
看到这个标题我相信大家应该并不陌生,一般在PC网络游戏中玩家通过鼠标左键在游戏世界中选择角色目标移动位置,接着主角将面朝点击的那个方向移动。首先就本文来说我们应当掌握的知识点是“鼠标拣选”。这是什么概念呢?其实很简单,就是玩家通过鼠标在Game视图中选择了一个点,需要得到该点在3D世界中的三维坐标系。Game视图是一个2D的平面,所以鼠标拣选的难点就是如何把一个2D坐标换算成3D坐标。我们可以使用射线的原理很好的解决这个问题,在平面中选择一个点后从摄像机向该点发射一条射线。判断:选择的这个点是否为地面,如果是地面拿到这个点的3D坐标即可。如下图所示,在场景视图中我们简单的制作了带坡度的地形,目标是用户点击带坡度或不带坡度的地形都可以顺利的到达目的地。
在Project视图中鼠标右键选择Import Package ->Script引入官方提供的脚本,这些脚本主要是应用于摄像机朝向的部分。首先在Hierarchy视图中选择摄像机组件,接着在导航栏菜单中选择Compont -> Camera-Control ->SmoothFollow脚本。实际意义是将跟随脚本绑定在摄像机之上,目的是主角移动后摄像机也能跟随主角一并移动。如下图所示,脚本绑定完毕后可在右侧监测面板视图中看到Smooth Follow脚本。Target 就是射向摄像机朝向的参照物,这里把主角对象挂了上去意思是摄像机永远跟随主角移动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0; // Place the script in the Camera-Control group in the component menu @script AddComponentMenu("Camera-Control/Smooth Follow") function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation //下面是原始代码。 //var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); //这里是我修改的,直接让它等于1, //摄像机就不会旋转。 var currentRotation = 1; // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
using UnityEngine; using System.Collections; public class Controller : MonoBehaviour { //人物的三个状态 站立、行走、奔跑 private const int HERO_IDLE = 0; private const int HERO_WALK = 1; private const int HERO_RUN = 2; //记录当前人物的状态 private int gameState = 0; //记录鼠标点击的3D坐标点 private Vector3 point; private float time; void Start () { //初始设置人物为站立状态 SetGameState(HERO_IDLE); } void Update () { //按下鼠标左键后 if(Input.GetMouseButtonDown(0)) { //从摄像机的原点向鼠标点击的对象身上设法一条射线 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //当射线彭转到对象时 if (Physics.Raycast(ray, out hit)) { //目前场景中只有地形 //其实应当在判断一下当前射线碰撞到的对象是否为地形。 //得到在3D世界中点击的坐标 point = hit.point; //设置主角面朝这个点,主角的X 与 Z轴不应当发生旋转, //注解1 transform.LookAt(new Vector3(point.x,transform.position.y,point.z)); //用户是否连续点击按钮 if(Time.realtimeSinceStartup - time <=0.2f) { //连续点击 进入奔跑状态 SetGameState(HERO_RUN); }else { //点击一次只进入走路状态 SetGameState(HERO_WALK); } //记录本地点击鼠标的时间 time = Time.realtimeSinceStartup; } } } void FixedUpdate() { switch(gameState) { case HERO_IDLE: break; case HERO_WALK: //移动主角 一次移动长度为0.05 Move(0.05f); break; case HERO_RUN: //奔跑时移动的长度为0.1 Move(0.1f); break; } } void SetGameState(int state) { switch(state) { case HERO_IDLE: //播放站立动画 point = transform.position; animation.Play("idle"); break; case HERO_WALK: //播放行走动画 animation.Play("walk"); break; case HERO_RUN: //播放奔跑动画 animation.Play("run"); break; } gameState = state; } void Move(float speed) { //注解2 //主角没到达目标点时,一直向该点移动 if(Mathf.Abs(Vector3.Distance(point, transform.position))>=1.3f) { //得到角色控制器组件 CharacterController controller = GetComponent<CharacterController>(); //注解3 限制移动 Vector3 v = Vector3.ClampMagnitude(point - transform.position,speed); //可以理解为主角行走或奔跑了一步 controller.Move(v); }else { //到达目标时 继续保持站立状态。 SetGameState(HERO_IDLE); } } } |
注解1:transform.LookAt()这个方法是设定主角对象的面朝方向,这里设定的方向是鼠标选择的目标点在游戏世界中点中的3D坐标。为了避免主角X与Z轴发生旋转(特殊情况)所以我们设定朝向的Y轴永远是主角自身的Y轴。
- 本文固定链接: https://www.xuanyusong.com/archives/841
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
为什么不用raycastall来监测所有碰撞到的对象,然后把其中的地面读取出来,这样也不会受阻挡物影响,不会朝阻挡物移动,因为射线也会和阻挡物背后的地面碰撞,这样就能去到正确的地方
大神,有一个问题请教。在MainCamera发出的射线,进行碰撞检测选择物体的时候,如果是Image的话,获得不到碰撞事件,是Image不支持吗?有什么方法可以检测出射线碰撞到了Image呢?
还有一点就是我需要动态加载地形 一次有可能加载四五块 500*500的 但是地形上有河流建筑等物体 所以导致加载很卡 有木有什么有效的方法解决这种卡顿呐。 雨松大大。
unity没有多线程, 所以要想不卡的话 只能减少每一帧实例化 的gameObject了
如果说我想点击屏幕一个点 然后我的角色会向点击方向发射一个子弹 请问怎么实现 我套用了移动的代码 但是好像不对。 Vector3 vpos3 = Camera.main.WorldToScreenPoint(transform.position); Vector2 vpos2 = new Vector2(vpos3.x, vpos3.y); //取得鼠标点击的屏幕坐标 Vector2 input = new Vector2(Input.mousePosition.x, Input.mousePosition.y); Vector2 normalied = ((vpos2 – input)).normalized; Vector3 targetDirection = new Vector3(normalied.x, 0.0f, normalied.y); cube.GetComponent().velocity = targetDirection;cube就相当于子弹。
包倒不进去,momo?都是英文路径呀
Vector3 movement = targetDirection * moveSpeed new Vector3 (0, verticalSpeed, 0) inAirVelocity;这句没理解.你直接告诉我计算出来的点的坐标
Vector3 movement = targetDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0) + inAirVelocity;这句没理解.你直接告诉我计算出来的点的坐标
一切都正常,就是如果点击高处建筑,人物就好在空中跑,这个怎么解决?
松哥,松哥,我用协程写移动的代码,为什么鼠标点的越快人物也移动的越快啊~void Update () {if(Input.GetMouseButtonDown(1)){_ray=_camera.ScreenPointToRay(Input.mousePosition);if(Physics.Raycast(_ray,out _hit)){if(_hit.collider.name==”Plane”){StartCoroutine(doMove());}}}}IEnumerator doMove(){ _endPoint=_hit.point;transform.LookAt (new Vector3 (_endPoint.x, transform.position.y, _endPoint.z));while(Vector3.Magnitude( _endPoint-_player.position)>0.5f){ _animator.Play(“Run”);_player.position =Vector3.ClampMagnitude(_endPoint-_player.position,0.1f);yield return null;}yield return 0;}
松哥,松哥,我用协程写移动的代码,为什么鼠标点的越快人物也移动的越快啊~ void Update () { if(Input.GetMouseButtonDown(1)) { _ray=_camera.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(_ray,out _hit)) { if(_hit.collider.name==”Plane”) { StartCoroutine(doMove()); } } } } IEnumerator doMove() { _endPoint=_hit.point; transform.LookAt (new Vector3 (_endPoint.x, transform.position.y, _endPoint.z)); while(Vector3.Magnitude( _endPoint-_player.position)>0.5f) { _animator.Play(“Run”); _player.position+=Vector3.ClampMagnitude(_endPoint-_player.position,0.1f); yield return null; } yield return 0; }
为啥角色移动,但是走路的动画就不播放呢?奇了怪了
雨松老师,package 无法解压 能不能发一个可以解压的呢?
下载后倒入不了Error while importing package: Couldn’t decompress package
雨松老师,请教您一个问题:我在示例中加入鼠标右键旋转视角的功能:(加入,修改js -MouseLook )结果:没有实现正确的效果。然后我把摄像机跟随移除,就可以了。能告诉我是什么原因吗?麻烦了,谢谢~
特别提醒下各位,如果你是直接复制代码的,一定要记得把文件编码方式改为utf8,否则就会出现很多错误,因为momo老师在里面写了很多注释,一定要记得,我在这个问题上花了好长时间!
楼主能否更正下错误代码呢?
为什么下载之后打不开啊?下载的package无论是win下面还是mac下面都无法解压,郁闷啊
MOMO老师,如果用C#的话,请问这段代码有什么问题呢?using UnityEngine;using System.Collections;public class cameraTarget : MonoBehaviour {public Transform target;public float height=10.0f;public float distance=5.0f;public float heightDamping=2.0f;public float rotationDamping=3.0f;// Use this for initializationvoid Start () {}// Update is called once per framevoid LateUpdate () {if(!target){return;}float wantedRotationAngle=target.eulerAngles.y;float wantedHeight=target.position.y height;float currentRotationAngle=target.eulerAngles.y;float currentHeight=transform.position.y;currentRotationAngle=Mathf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime);currentHeight=Mathf.Lerp(currentHeight,wantedHeight,heightDamping*Time.deltaTime);float currentRotation=1;transform.position=target.position;transform.position-=currentRotation*Vector3.forward*Time.deltaTime;transform.position.y=currentHeight;transform.LookAt(target);}}
MOMO老师,如果用C#的话,请问这段代码有什么问题呢?using UnityEngine;using System.Collections;public class cameraTarget : MonoBehaviour { public Transform target; public float height=10.0f; public float distance=5.0f; public float heightDamping=2.0f; public float rotationDamping=3.0f; // Use this for initialization void Start () { } // Update is called once per frame void LateUpdate () { if(!target){ return; } float wantedRotationAngle=target.eulerAngles.y; float wantedHeight=target.position.y+height; float currentRotationAngle=target.eulerAngles.y; float currentHeight=transform.position.y; currentRotationAngle=Mathf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime); currentHeight=Mathf.Lerp(currentHeight,wantedHeight,heightDamping*Time.deltaTime); float currentRotation=1; transform.position=target.position; transform.position-=currentRotation*Vector3.forward*Time.deltaTime; transform.position.y=currentHeight; transform.LookAt(target); }}
确实解压不了,Unity直接挂了。。。3.5.6和4.0的都试过了。。。。
http://vdisk.weibo.com/s/abU_3 momo这里下载的package无论是win下面还是mac下面都无法解压, 好像出问题了, 有空能不能重新传一个呢~~~
是不是中文路径?
不是路径的问题 mac下面也是解压不了, 你下载一下试试看, 我下载了两次都不行,其他的都可以的, 要不就是下载的时候文件损坏了,用rar也打不开的
controller.cs出現了以下的錯誤, 請幫忙阿MOMO老師,感謝!Assets/Standard Assets/Character Controllers/Sources/Scripts/controller.cs(45,37): error CS1525: Unexpected symbol
else'Assets/Standard Assets/Character Controllers/Sources/Scripts/controller.cs(56,14): error CS0116: A namespace can only contain types and namespace declarationsAssets/Standard Assets/Character Controllers/Sources/Scripts/controller.cs(77,14): error CS0116: A namespace can only contain types and namespace declarationsAssets/Standard Assets/Character Controllers/Sources/Scripts/controller.cs(104,21): error CS1525: Unexpected symbol
else’Assets/Standard Assets/Character Controllers/Sources/Scripts/controller.cs(107,32): error CS8025: Parsing errorcontroller 代码有错误。 检查一下else附近的代码。、
我直接複製老師您的代碼貼上的, 不知這錯誤如何解決呢? 不好意思阿老師, 我不太會檢查程序.
else 前面有个错误
调用这个controller.Move(v);的时候提示Object reference not set to an instance of an objectController.Move (Single speed) (at Assets/Controller.cs:91)求解?
旧的射线法没有问题。对新的方法,不太理解怎么用。targetDirection = Quaternion.Euler(0f,y – 180.0f, 0f) * targetDirection;到这里,targetDrection是否是角色最终要移动的向量?还是仅仅是移动的方向?后面的调用Move方法,更是不理解:ector3 movement = targetDirection * moveSpeed new Vector3(0, verticalSpeed, 0) inAirVelocity;其中verticalSpeed和inAirVelocity应该怎么设置?不跳的话是否都可以不加这两部分的向量?最后,如何判断走到了目标地点?这个是让我最不理解的,因为没有记录目标点,所以不知道如何判断结束移动。。。求详解啊
旧的射线法没有问题。对新的方法,不太理解怎么用。targetDirection = Quaternion.Euler(0f,y – 180.0f, 0f) * targetDirection;到这里,targetDrection是否是角色最终要移动的向量?还是仅仅是移动的方向?后面的调用Move方法,更是不理解:ector3 movement = targetDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;其中verticalSpeed和inAirVelocity应该怎么设置?不跳的话是否都可以不加这两部分的向量?最后,如何判断走到了目标地点?这个是让我最不理解的,因为没有记录目标点,所以不知道如何判断结束移动。。。求详解啊
你看一下角色控制器把。。 角色控制器 控制移动的关键字就是 movement 你查一下代码。
Unity3D研究院之处理摄像机跟随避免相机穿墙拉近的方法(四十四) 这里面有例子噢。
啊!又有新的了!谢谢啦
还是我。。。新的方法我也实验了,不知道我的理解对不对:射线法是直接取得目的地的position,每一帧判断应该移动的坐标以及是否抵达;而2D法是获得移动的方向,每帧通过方向和移动速度来计算移动的坐标,并不知道终点在哪里。所以2D法只能用于“按着屏幕走路”的情况;如果想点一下就移动到点的地方,就只能用射线法了。
是的 ,得用射线的方法,,射线有一个方法可以得到.Physics.RaycastAll 用这个方法得到射线碰撞的所有点。。
正需要这个呢!多谢!我去看reference
在导出一下。。。BUG吧
雨松老师,请问怎样才能让角色在起伏地形上行走呢,按您的步骤做了之后,移动都是直线的,能让他有重力,正常点多动吗?
可以做一个碰撞让他上升到一定位置后不在上升
角色控制器就可以的
雨松老师,请问,我按照你的步骤做了之后,一直出现 error CS0165: Use of unassigned local variable `hit’ 的错误,是怎么回事呢?
未声明的标示符号吧 ,check check
MOMO 老师。。不太理解Mathf.Abs(Vector3.Distance(point, transform.position))>=1.3f 这里是不是应该<=1.3f? Vector3.ClampMagnitude 不太理解这个函数的含义。。看手册。返回向量的长度,最大不超过maxLength所指示的长度。 ? 不太理解。
这个是绝对值两个点之间的位置。
Vector3 v = Vector3.ClampMagnitude(point – transform.position,speed); v这个返回的为什么是(0.0,0.0,0.0)?那controller.Move(v);不就没移动吗?但是却可以!我换了一个模型自己做的角色动画,走,跑,但是使用Controller脚本,模型只是原地走,原地跑?
Unity3D_24.unitypackage 工程导入报错Error while importing package: Couldn’t decompress package路径没有中文啊
請問要是想在Unity Android上寫一個用按鈕來控制人物移動跳躍 要怎麼寫我寫了一個跳躍 可是我不知道要怎麼讓他跳下來地上 請問要怎麼寫if (Input.GetMouseButton(0)){ if (butJump.HitTest(Input.mousePosition)) { controller.Move((Vector3.up) * jumpSpeed); }}
請問…我在家自己做一個這樣的,可是我如果一直點他的頭或是點高一點的建築物,他會升天不會掉下來地面,我用了rigidbody也沒用
角色控制器组件 是不需要刚体就可以感应物理引擎效果的。
MOMO大,Physics.Raycast(ray, out hit)中的关键字out是什么意思
支持老乡
momo加油
感谢支持 嘿嘿
文章不错,楼主辛苦了~~
雨大你好 我现在遇到一个问题 就是使用角色控制组件时 controller.move() 即使在平面 角色X轴也会产生轻微的倾斜 使得挂在角色上的物体也随之倾斜 这点是不必要的 请问如何解决
辛苦了