逐点运动

1.移动到鼠标点击处停止

描述:物体cube运动到鼠标点击处并停止运动

在上节基础上我们增添了这些内容:

首先,定义3个私有变量,鼠标点击位置endPoint(Vector3类型)、物体cube距点击处的距离长度s(float类型)、每帧cube移动的距离长度dis(float类型);

然后,在Update()函数中,第一个if条件判断中添加:获取endPoint位置,计算s长度,令dis为0;第二个if条件判断中添加:每帧dis累加,判断dis是否不小于s,若dis大于等于s,说明cube移动到点击处,使moveFlag置为0不再移动,并将endPoint赋给cube的位置,使得cube最终停止在该位置。

using UnityEngine;

using System.Collections;

public class LineMove : MonoBehaviour

{

public GameObject cube;

private Camera _camera;

private Vector3 screenV;

private float hudu;

private float speed = 3;

private float dx;

private float dy;

private int moveflag = 0;

private Vector3 endPoint;

private float s;

private float dis;

void Start()

{

_camera = Camera.main;

screenV = _camera.WorldToScreenPoint(cube.transform.position);

}

void Update ()

{

if(Input.GetMouseButtonDown(0))

{

Vector3 dianV = Input.mousePosition;

Vector3 cubePosition = cube.transform.position;

dianV.z = screenV.z;

Vector3 wv = _camera.ScreenToWorldPoint(dianV);

endPoint = wv;

float ddx = wv.x - cubePosition.x;

float ddy = wv.y - cubePosition.y;

s = Mathf.Sqrt(ddx * ddx + ddy * ddy);

hudu = Mathf.Atan2(ddy, ddx);

dx = speed * Mathf.Cos(hudu);

dy = speed * Mathf.Sin(hudu);

dis = 0;

moveflag = 1;

}

if (moveflag == 1)

{

cube.transform.Translate(Vector3.right * Time.deltaTime * dx);

cube.transform.Translate(Vector3.up * Time.deltaTime * dy);

dis += speed * Time.deltaTime;

if (dis >= s)

{

moveflag = 0;

cube.transform.position = endPoint;

}

}

}

}

2.逐点运动(静态数组)

描述:物体cube连续运动到事先设定好的3个坐标处,并在最后一个坐标位置处停止运动

首先,由于存储在静态数组中,因此定义私有变量:静态数组pArr(Vector3类型)、数组长度len(int类型)、数组下标pIndex(int类型)并初始化为0。

然后,在Start()函数中初始化数组pArr,还有长度len,并调用moveCount()函数;

moveCount()函数,是cube移动到pArr[pIndex]点处,类似上个程序中判断是否鼠标按下中执行的程序。先获取即将要移动到的点p和当前cube的位置坐标v,接着计算这两坐标的距离s,然后算出弧度hudu就可以计算出速度在X轴和Y轴上的分量speedX和speedY,最后令dis为0即每次有新坐标时dis重新计算,且moveFlag为1即cube可以移动;

最后,在Update()函数中,先判断moveFlag是否为1,若为1则可以移动,执行以下操作。使cube在X、Y轴运动,并每帧dis累加,这时就需要判断,当dis大于等于s时,说明cube已经到达该坐标点进而执行pIndex++获取下一个数组下标,但这时的坐标点是最后一个坐标吗,还需判断pIndex是否小于数组长度len,当小于时说明pIndex在数组中,应该继续调用moveCount()函数,计算cube与pArr[pIndex]之间的属性;否则令moveFlag为0,即当前已是最后一个坐标,cube不再移动。

using UnityEngine;

using System.Collections;

public class PointToPoint : MonoBehaviour

{

public GameObject moveCube;

private Vector3[] pArr;

private int len;

private float speed = 3;

private int pIndex = 0; //移动到的数组下标

private float s;

private float speedX;

private float speedY;

private int moveFlag = 0;

private float dis;

void Start ()

{

pArr = new Vector3[3];

pArr[0] = new Vector3(0, 0, 0);

pArr[1] = new Vector3(-3, 2, 0);

pArr[2] = new Vector3(4, 1, 0);

len = pArr.Length;

moveCount();

}

void moveCount()

{

Vector3 p = pArr[pIndex];

Vector3 v = moveCube.transform.position;

float dx = p.x - v.x;

float dy = p.y - v.y;

s = Mathf.Sqrt(dx * dx + dy * dy); //两点间的距离

float hudu = Mathf.Atan2(dy, dx);

speedX = speed * Mathf.Cos(hudu);

speedY = speed * Mathf.Sin(hudu);

dis = 0;

moveFlag = 1;

}

void Update ()

{

if (moveFlag == 1)

{

moveCube.transform.Translate(Vector3.right * Time.deltaTime * speedX);

moveCube.transform.Translate(Vector3.up * Time.deltaTime * speedY);

dis += speed * Time.deltaTime;

if (dis >= s)

{

pIndex++;

if (pIndex < len)

{

moveCount();

}

else

{

moveFlag = 0;

}

}

}

}

}

3.动态增删节点运动(动态数组)

描述:鼠标在屏幕上连续点击,cube会依次移动到这些点击位置并在最后一个坐标处停止运动

动态数组

ArrayList:将添加的所有数据都当做Object类型来处理,所以不是安全类型,在使用时会有装箱拆箱操作,这会有很大的性能损耗。

ArrayList arr = new ArrayList(); //定义数组

arr.Add(1); //添加整型

arr.Add("hello"); //添加字符串型

arr.Add(new Vector3(1,1,1)); //添加Object类型

int len = arr.Count; //数组长度

List:由于它是泛型,因此避免了ArrayList的缺陷,因此大多数执行的更好也安全。

using System.Collections.Generic; //注意要引入

List arr = new List(); //定义int型数组

arr.Add(1); //只能添加int型

int len = arr.Count(); //数组长度

arr.RemoveAt(0); //删除数组第0个数

首先,定义私有变量:Vector3类型的泛型数组pArr、int类型的数组长度len;

接着,在Start()函数中,给pArr开辟空间,主摄像机和cube的屏幕坐标与前几节一样;

然后,在moveCount()函数中,获取数组第一个元素pArr[0]赋给p,经过一系列的弧度、速度分量计算后,移除数组的第一个元素,这样保证数组不会过大而导致的运行卡顿;

最后,在Update()函数,第一个if条件判断中,得到鼠标点击位置的世界坐标wv,并添加到数组pArr中,再调用moveCount(),但需要注意的是,这时如果点击过快,那么cube还没来得及移动到第一个点就要拐到第二个点,所以还需这样处理,判断moveFlag是否为0,若为0则说明cube已到达上一个点,则调用moveCount()来计算下一个点;若为1,则不调用moveCount();第二个if条件判断中,如果dis大于等于s,则cube已到达点上,继续判断是否是最后一个点,这时看数组长度pArr.Count,当大于0,说明数组中还有元素即还有点,则调用moveCount();否则该点是最后一个点,停止运动。

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class PointArrMove : MonoBehaviour

{

public GameObject moveCube;

private List pArr;

private int len;

private Camera _camera;

private Vector3 screenV;

private float speed = 3;

private float s;

private float speedX;

private float speedY;

private float hudu;

private int moveFlag = 0;

private float dis;

void Start ()

{

pArr = new List();

_camera = Camera.main;

screenV = _camera.WorldToScreenPoint(moveCube.transform.position);

}

void moveCount()

{

Vector3 p = pArr[0];

Vector3 v = moveCube.transform.position;

float dx = p.x - v.x;

float dy = p.y - v.y;

s = Mathf.Sqrt(dx * dx + dy * dy); //两点间的距离

float hudu = Mathf.Atan2(dy, dx);

speedX = speed * Mathf.Cos(hudu);

speedY = speed * Mathf.Sin(hudu);

dis = 0;

pArr.RemoveAt(0);

moveFlag = 1;

}

void Update ()

{

if (Input.GetMouseButtonDown(0))

{

Vector3 dianV = Input.mousePosition;

dianV.z = screenV.z;

Vector3 wv = _camera.ScreenToWorldPoint(dianV);

pArr.Add(wv);

if (moveFlag == 0) //判断运动到点再转向另一点运动

{

moveCount();

}

}

if (moveFlag == 1)

{

moveCube.transform.Translate(Vector3.right * Time.deltaTime * speedX);

moveCube.transform.Translate(Vector3.up * Time.deltaTime * speedY);

dis += speed * Time.deltaTime;

if (dis >= s)

{

if (pArr.Count > 0)

{

moveCount();

}

else

{

moveFlag = 0;

}

}

}

}

}

uniny 物体运动到一个点停止_Unity3D中的逐点运动相关推荐

  1. 物体运动到一个点停止_运用SolidWorks运动仿真来做的最速降线及其验证,来看看我的办法...

    一个仅受重力的物体,从一个点出发,沿着一条没有摩擦的斜坡滚动到另外一个点.肯定有一个斜坡使物体运动的时间最短.这个斜坡所在的曲线就是"最速降线". 关于这个最速降线是怎么计算出来, ...

  2. 物体运动到一个点停止_教科版五年级上册第四单元运动和力复习要点

    培养科学思维  ◇  丰收科学田   ◇ 涵养科学精神 第四单元<运动和力> 复习要点 第1课  我们的小缆车 1.我们提水,感到水桶对手有(向下)的拉力:我们背书包,感到书包对肩部有(向 ...

  3. uniny 物体运动到一个点停止_隐藏的几何:各类随机物体中的深层联系

    在"SLE曲线"结构中,随机性增加 | 来源:Jason Miller 标准几何体能够用简单规则描述,比如通过y = ax + b定义每条直线,而且各要素之间的关系也相对明了:两点 ...

  4. uniny 物体运动到一个点停止_海洋科学导论复习题

    的一侧.由于月球引力的作用形成高潮:E)地球上最靠近太阳的一侧.由于太阳引力的作用形成高潮 18.关于太阳和月球引潮力的描述,那些正确? A)由于其质量大,太阳引潮力是月球引潮力的2倍:B)月球引起的 ...

  5. uniny 物体运动到一个点停止_人教版高中英语必修五Unit 5 单词详解

    Unit 5 1.aid n. & vt.帮助:援助:资助 with the aid of在-的帮助下,在-援助下 give aid to给-予帮助 first aid急救:急救护理 aid ...

  6. 物体运动到一个点停止_大颗粒搭建中常见的结构运动

    今天,给大家带来大颗粒搭建中常见的结构 家长们可以带孩子一起将原理编程实践 举一反三,创造创新! 1齿轮传动 齿轮传动是指用主.从动轮的齿轮进行动力传递,从而达到使物体运动的作用. ①  减速传动 当 ...

  7. Jacobsen v. Katzer:开源运动的一个重大胜利

    今天早上起床,看到Lawrence Lessig(斯坦福大学法学院教授,CC许可证创始人)在Blog上宣布一个"huge and important news". 昨天历史上第一次 ...

  8. Cardboard虚拟现实开发技巧(一)之放置一个固定在视野中的物体

    Google Cardboard 虚拟现实眼镜开发技巧(一)之放置一个固定在视野中的物体 利用CardboardMain下的Head轻松放置一个固定在视野中的物体 大家知道在游戏开发中,我们经常会需要 ...

  9. opengl 粒子按轨迹运动_袁讲经典4:一个粒子在电场中的运动轨迹相关问题

    袁讲经典4:一个粒子在电场中的运动轨迹相关问题 如上图,带电粒子在电场中(电场线如图)从A运动到B,则: 1.判断A和B处的受力大小和加速度大小 电场力大小 电场线密的地方电场强度大 A处的电场强度小 ...

最新文章

  1. AsyncQueryHandler了解
  2. (视频+图文)机器学习入门系列-第2章 线性回归
  3. 信用卡还不起会有什么严重后果?
  4. apache hadoop_使用Apache Hadoop计算PageRanks
  5. 指针和引用的区别和联系
  6. Linux通过进程号查询占用的端口
  7. 操作系统实验--存储管理
  8. 计算机等级和计算应用区别,2020上半年软考问答:计算机等级考试和软考有什么区别?...
  9. layuit 框架_UI框架Layui入门介绍
  10. Vin码识别即车架号识别
  11. Calibre转换电子书格式
  12. css中的counter计数器
  13. Get the information of all heroes in the League of Legends through the crawler.
  14. 程序员全职接单一个月的感触
  15. 云控系统php源码,xrkmontor字符云监控系统php源码 v2.5
  16. Python编程语言体现出的设计模式
  17. 【IDEA】idea插件的安装和删除
  18. 【软件测试】什么软件测试,软件测试和研发的区别
  19. 大数据核心技术是什么
  20. 高性能的gpu服务器,高性能GPU云服务器

热门文章

  1. Serverless 工程实践 | 细数 Serverless 的配套服务
  2. 兰州2021高考一诊成绩查询,2021兰州中考"一诊"成绩分析结果查询
  3. The world at your fingertips — 天涯明月刀幕后23(海战)
  4. 非人哉恰饭的九月最美丽,只有啸天是菜鸡,上班睡觉太厉害了
  5. Python编程专属骚技巧1
  6. ASP.NET Core微服务(四)——【静态vue使用axios解析接口】
  7. Spring boot 上传文件时 MultipartFile 报空指针
  8. 阿里、美团、拼多多、网易大厂面试之Redis+多线程+JVM+微服务...
  9. 修改mac的hosts文件
  10. 配置启动MySQL的Docker容器