uniny 物体运动到一个点停止_Unity3D中的逐点运动
逐點運動
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中的逐点运动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mug网络用语_下面这些短语你尽管翻译,
- 下一篇: 一年中最后一个月的最后一天说说_新的一年