Unity實(shí)現(xiàn)物體左右移動(dòng)效果
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)物體左右移動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
效果如下

代碼:
using UnityEngine;
using System.Collections;
//Add this script to the platform you want to move.
//左右移動(dòng)的平臺(tái)
public class MovingPlatform : MonoBehaviour {
//Platform movement speed.平臺(tái)移動(dòng)速度
public float speed;
//This is the position where the platform will move.平臺(tái)移動(dòng)的位置
public Transform MovePosition;//創(chuàng)建一個(gè)空物體作為移動(dòng)的位置
private Vector3 StartPosition;
private Vector3 EndPosition;
private bool OnTheMove;
// Use this for initialization
void Start () {
//Store the start and the end position. Platform will move between these two points.儲(chǔ)存左右兩端點(diǎn)位置
StartPosition = this.transform.position;
EndPosition = MovePosition.position;
}
void FixedUpdate () {
float step = speed * Time.deltaTime;
if (OnTheMove == false) {
this.transform.position = Vector3.MoveTowards (this.transform.position, EndPosition, step);
}else{
this.transform.position = Vector3.MoveTowards (this.transform.position, StartPosition, step);
}
//When the platform reaches end. Start to go into other direction.
if (this.transform.position.x == EndPosition.x && this.transform.position.y == EndPosition.y && OnTheMove == false) {
OnTheMove = true;
}else if (this.transform.position.x == StartPosition.x && this.transform.position.y == StartPosition.y && OnTheMove == true) {
OnTheMove = false;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達(dá)式樹實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#實(shí)現(xiàn)窗體中動(dòng)態(tài)按鈕的設(shè)計(jì)方法
在窗體界面中,通常以按鈕來代替菜單欄的功能,這種形式雖然給用戶一種直觀、界面風(fēng)格各異的感覺,但通常按鈕都是以靜止的形式顯示,所以本文給大家介紹了C#實(shí)現(xiàn)窗體中動(dòng)態(tài)按鈕的設(shè)計(jì)方法,感興趣的朋友可以參考下2024-04-04
C#使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器的示例詳解
以往一般都是用 System.Timers.Timer 來做計(jì)時(shí)器,其實(shí) System.Threading.Timer 也可以實(shí)現(xiàn)計(jì)時(shí)器功能,下面就跟隨小編一起來學(xué)習(xí)一下如何使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器功能吧2024-01-01
WPF開發(fā)txt閱讀器實(shí)現(xiàn)目錄提取功能
這篇文章主要為大家詳細(xì)介紹了WPF開發(fā)txt閱讀器時(shí)如何實(shí)現(xiàn)目錄提取功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法
這篇文章主要介紹了C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法,涉及網(wǎng)絡(luò)文件操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密
這篇文章主要介紹了C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密,需要的朋友參考下2017-01-01
WPF使用WrapPanel實(shí)現(xiàn)虛擬化效果
這篇文章主要為大家詳細(xì)介紹了如何利用WPF WrapPanel實(shí)現(xiàn)虛擬化效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09
C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本,實(shí)例分析了C#基于鏈表實(shí)現(xiàn)的記事本功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

