Unity實(shí)現(xiàn)物體左右移動(dòng)效果
更新時(shí)間:2019年08月15日 11:22:36 作者:_April_
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體左右移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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#中把日志導(dǎo)出到txt文本的簡(jiǎn)單實(shí)例
這篇文章介紹了C#中把日志導(dǎo)出到txt文本的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10C#在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件
這篇文章主要為大家詳細(xì)介紹了C#如何在Windows上調(diào)用7-zip實(shí)現(xiàn)壓縮文件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10C#中的Timer和DispatcherTimer使用實(shí)例
這篇文章主要介紹了C#中的Timer和DispatcherTimer使用實(shí)例,本文分別給出它們的使用代碼實(shí)例,需要的朋友可以參考下2015-01-01.NET實(shí)現(xiàn):將EXE設(shè)置開(kāi)機(jī)自動(dòng)啟動(dòng)
.NET實(shí)現(xiàn):將EXE設(shè)置開(kāi)機(jī)自動(dòng)啟動(dòng)的方法,需要的朋友可以參考一下2013-03-03