博客
关于我
unity制作类似红警控制效果
阅读量:590 次
发布时间:2019-03-11

本文共 6216 字,大约阅读时间需要 20 分钟。

在unity的开发中如果想控制一个物体的用鼠标左右键点击地面而发生移动的效果很简单

用射线很容易可以实现
如果想要实现类似于红警 魔兽之类的游戏控制效果 就稍微复杂一些
通过鼠标的拖拽 拖拽范围内物体同时受到鼠标控制的影响

1.玩家脚本

先给出一个玩家脚本(也就是挂在可收到鼠标控制的物体上)

public class AI : MonoBehaviour{       public bool CanMove = false;//是否可以受到影响(默认为否)    public Vector3 Pos;//位置(在另一个脚本中会赋值)    //注意:上边两个变量虽然不用在unity中赋值 但是在另一个脚本中需要调用 所以是public类型的    private void Update()    {           if (CanMove)//允许受到控制        {               Move(Pos );        }    }    private void Move(Vector3 TargetPos)//移动(移动目标点位置)    {           Pos.y = transform.position.y;        if(Vector3.Distance(transform.position, TargetPos) > 0.1f)        //目标点与现在物体的位置的距离大于0.1,(物体未与目标重合)          {               transform.LookAt(TargetPos);            //动态改变物体方向            transform.Translate(Vector3.forward * Time.deltaTime * 3);            //朝着目标点移动        }    }}

注释写的很清楚

很难的代码也没有

2.挂在摄像头上脚本

public class MouseControl : MonoBehaviour{       private Vector3 FirstPos;//鼠标开始按下位置    private Vector3 SecondPos;//鼠标结束抬起位置    private Vector3 Pos;    private Vector3 TempPos;//临时变量(位置)    public List
list; private void Start() { list = new List
();//给private类型变量赋值 } private void Update() { GetObject(); if (Input.GetMouseButtonDown(0))//鼠标按下 { Pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10)); FirstPos = Pos;//初始位置赋值 } if (Input.GetMouseButton(0))//鼠标持续按下 { Pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10)); SecondPos = Pos;//结束位置赋值 } if (Input.GetMouseButton(1)) { if (list.Count != 0) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { TempPos = hit.point; } foreach (var p in list) { p.GetComponent
().CanMove = true; //设置AI中的CanMove为Ture p.GetComponent
().Pos = TempPos; //给AI脚本中变量赋值 } } } } void GetObject() { Vector3 Centers=(FirstPos+SecondPos)/2; Collider[] col = Physics.OverlapBox(Centers,new Vector3( Mathf.Abs(FirstPos.x-SecondPos.x), Mathf.Abs(FirstPos.x-SecondPos.x), Mathf.Abs(FirstPos.z-SecondPos.z))); foreach (var o in col) { o.GetComponent
().material.color = Color.green ; if (o.tag == "Player" && !list.Contains(o.gameObject)) //带有player的标签的物体 并且list不包含该物体 { list.Add(o.gameObject);//该物体添加到list中 } } } void OnDrawGizmos()//为了可视化(没有也可以) { Gizmos.color = Color.green; Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, FirstPos.y, FirstPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, FirstPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, SecondPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, SecondPos.z), new Vector3(FirstPos.x, FirstPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, SecondPos.z), new Vector3(FirstPos.x, SecondPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(FirstPos.x, SecondPos.y, FirstPos.z)); Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, FirstPos.z)); Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, SecondPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, FirstPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(SecondPos.x, SecondPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(FirstPos.x, FirstPos.y, SecondPos.z)); Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, FirstPos.z), new Vector3(FirstPos.x, SecondPos.y, SecondPos.z)); }}

里边有关于射线检测的知识 我没有注释 如果不懂可以去我的以前的博客中查看

然后里边的Physics.OverlapBox(中心点位置,大小);
类似于球的检测(球的是半径)
然后

void OnDrawGizmos()//为了可视化(没有也可以)    {           Gizmos.color = Color.green;        Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, FirstPos.y, FirstPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, FirstPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, SecondPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, SecondPos.z), new Vector3(FirstPos.x, FirstPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, SecondPos.z), new Vector3(FirstPos.x, SecondPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(FirstPos.x, SecondPos.y, FirstPos.z));        Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, FirstPos.z));        Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, SecondPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(SecondPos.x, FirstPos.y, FirstPos.z), new Vector3(SecondPos.x, FirstPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(SecondPos.x, SecondPos.y, FirstPos.z), new Vector3(SecondPos.x, SecondPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, FirstPos.y, FirstPos.z), new Vector3(FirstPos.x, FirstPos.y, SecondPos.z));        Gizmos.DrawLine(new Vector3(FirstPos.x, SecondPos.y, FirstPos.z), new Vector3(FirstPos.x, SecondPos.y, SecondPos.z));    }

只能在scene面板观察到

3.效果图

在scene面板的Cube

在这里插入图片描述
在unity中简单赋值之后即可范围性控制物体的移动

转载地址:http://knqtz.baihongyu.com/

你可能感兴趣的文章
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>