uniny 物体运动到一个点停止_Unity3D中的逐点运动
逐點(diǎn)運(yùn)動(dòng)
1.移動(dòng)到鼠標(biāo)點(diǎn)擊處停止
描述:物體cube運(yùn)動(dòng)到鼠標(biāo)點(diǎn)擊處并停止運(yùn)動(dòng)
在上節(jié)基礎(chǔ)上我們?cè)鎏砹诉@些內(nèi)容:
首先,定義3個(gè)私有變量,鼠標(biāo)點(diǎn)擊位置endPoint(Vector3類型)、物體cube距點(diǎn)擊處的距離長(zhǎng)度s(float類型)、每幀cube移動(dòng)的距離長(zhǎng)度dis(float類型);
然后,在Update()函數(shù)中,第一個(gè)if條件判斷中添加:獲取endPoint位置,計(jì)算s長(zhǎng)度,令dis為0;第二個(gè)if條件判斷中添加:每幀dis累加,判斷dis是否不小于s,若dis大于等于s,說明cube移動(dòng)到點(diǎn)擊處,使moveFlag置為0不再移動(dòng),并將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.逐點(diǎn)運(yùn)動(dòng)(靜態(tài)數(shù)組)
描述:物體cube連續(xù)運(yùn)動(dòng)到事先設(shè)定好的3個(gè)坐標(biāo)處,并在最后一個(gè)坐標(biāo)位置處停止運(yùn)動(dòng)
首先,由于存儲(chǔ)在靜態(tài)數(shù)組中,因此定義私有變量:靜態(tài)數(shù)組pArr(Vector3類型)、數(shù)組長(zhǎng)度len(int類型)、數(shù)組下標(biāo)pIndex(int類型)并初始化為0。
然后,在Start()函數(shù)中初始化數(shù)組pArr,還有長(zhǎng)度len,并調(diào)用moveCount()函數(shù);
moveCount()函數(shù),是cube移動(dòng)到pArr[pIndex]點(diǎn)處,類似上個(gè)程序中判斷是否鼠標(biāo)按下中執(zhí)行的程序。先獲取即將要移動(dòng)到的點(diǎn)p和當(dāng)前cube的位置坐標(biāo)v,接著計(jì)算這兩坐標(biāo)的距離s,然后算出弧度hudu就可以計(jì)算出速度在X軸和Y軸上的分量speedX和speedY,最后令dis為0即每次有新坐標(biāo)時(shí)dis重新計(jì)算,且moveFlag為1即cube可以移動(dòng);
最后,在Update()函數(shù)中,先判斷moveFlag是否為1,若為1則可以移動(dòng),執(zhí)行以下操作。使cube在X、Y軸運(yùn)動(dòng),并每幀dis累加,這時(shí)就需要判斷,當(dāng)dis大于等于s時(shí),說明cube已經(jīng)到達(dá)該坐標(biāo)點(diǎn)進(jìn)而執(zhí)行pIndex++獲取下一個(gè)數(shù)組下標(biāo),但這時(shí)的坐標(biāo)點(diǎn)是最后一個(gè)坐標(biāo)嗎,還需判斷pIndex是否小于數(shù)組長(zhǎng)度len,當(dāng)小于時(shí)說明pIndex在數(shù)組中,應(yīng)該繼續(xù)調(diào)用moveCount()函數(shù),計(jì)算cube與pArr[pIndex]之間的屬性;否則令moveFlag為0,即當(dāng)前已是最后一個(gè)坐標(biāo),cube不再移動(dòng)。
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; //移動(dòng)到的數(shù)組下標(biāo)
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); //兩點(diǎn)間的距離
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.動(dòng)態(tài)增刪節(jié)點(diǎn)運(yùn)動(dòng)(動(dòng)態(tài)數(shù)組)
描述:鼠標(biāo)在屏幕上連續(xù)點(diǎn)擊,cube會(huì)依次移動(dòng)到這些點(diǎn)擊位置并在最后一個(gè)坐標(biāo)處停止運(yùn)動(dòng)
動(dòng)態(tài)數(shù)組
ArrayList:將添加的所有數(shù)據(jù)都當(dāng)做Object類型來處理,所以不是安全類型,在使用時(shí)會(huì)有裝箱拆箱操作,這會(huì)有很大的性能損耗。
ArrayList arr = new ArrayList(); //定義數(shù)組
arr.Add(1); //添加整型
arr.Add("hello"); //添加字符串型
arr.Add(new Vector3(1,1,1)); //添加Object類型
int len = arr.Count; //數(shù)組長(zhǎng)度
List:由于它是泛型,因此避免了ArrayList的缺陷,因此大多數(shù)執(zhí)行的更好也安全。
using System.Collections.Generic; //注意要引入
List arr = new List(); //定義int型數(shù)組
arr.Add(1); //只能添加int型
int len = arr.Count(); //數(shù)組長(zhǎng)度
arr.RemoveAt(0); //刪除數(shù)組第0個(gè)數(shù)
首先,定義私有變量:Vector3類型的泛型數(shù)組pArr、int類型的數(shù)組長(zhǎng)度len;
接著,在Start()函數(shù)中,給pArr開辟空間,主攝像機(jī)和cube的屏幕坐標(biāo)與前幾節(jié)一樣;
然后,在moveCount()函數(shù)中,獲取數(shù)組第一個(gè)元素pArr[0]賦給p,經(jīng)過一系列的弧度、速度分量計(jì)算后,移除數(shù)組的第一個(gè)元素,這樣保證數(shù)組不會(huì)過大而導(dǎo)致的運(yùn)行卡頓;
最后,在Update()函數(shù),第一個(gè)if條件判斷中,得到鼠標(biāo)點(diǎn)擊位置的世界坐標(biāo)wv,并添加到數(shù)組pArr中,再調(diào)用moveCount(),但需要注意的是,這時(shí)如果點(diǎn)擊過快,那么cube還沒來得及移動(dòng)到第一個(gè)點(diǎn)就要拐到第二個(gè)點(diǎn),所以還需這樣處理,判斷moveFlag是否為0,若為0則說明cube已到達(dá)上一個(gè)點(diǎn),則調(diào)用moveCount()來計(jì)算下一個(gè)點(diǎn);若為1,則不調(diào)用moveCount();第二個(gè)if條件判斷中,如果dis大于等于s,則cube已到達(dá)點(diǎn)上,繼續(xù)判斷是否是最后一個(gè)點(diǎn),這時(shí)看數(shù)組長(zhǎng)度pArr.Count,當(dāng)大于0,說明數(shù)組中還有元素即還有點(diǎn),則調(diào)用moveCount();否則該點(diǎn)是最后一個(gè)點(diǎn),停止運(yùn)動(dòng)。
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); //兩點(diǎn)間的距離
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) //判斷運(yùn)動(dòng)到點(diǎn)再轉(zhuǎn)向另一點(diǎn)運(yùn)動(dòng)
{
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;
}
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的uniny 物体运动到一个点停止_Unity3D中的逐点运动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mug网络用语_下面这些短语你尽管翻译,
- 下一篇: 图像坐标球面投影_坐标系统及投影概述