Unity實現(xiàn)簡單場景分層移動
本文實例為大家分享了Unity實現(xiàn)簡單場景分層移動的具體代碼,供大家參考,具體內(nèi)容如下
前言
開發(fā)游戲經(jīng)常需要用到把前景、場景、背景等不同層級的物體進行不同速度的移動以實現(xiàn)真實感。
效果
云、建筑、地面、前景植被各層次場景分層移動。

代碼
using UnityEngine;
public class DistantView : MonoBehaviour
{
public GameObject follow;
public float scaleOffset;
public bool isHorizontal = true;
public bool isVertical = true;
Vector2 pos;
Vector2 followPos;
float offsetX;
float offsetY;
private void Start()
{
if (follow != null)
followPos = follow.transform.localPosition;
}
void LateUpdate()
{
if (follow!=null)
{
pos = transform.localPosition;
if (isHorizontal)
{
offsetX = (follow.transform.localPosition.x - followPos.x) * scaleOffset;
pos.x += offsetX;
}
if (isVertical)
{
pos.y += offsetY;
offsetY = (follow.transform.localPosition.y - followPos.y) * scaleOffset;
}
transform.localPosition = pos;
followPos = follow.transform.localPosition;
}
}
}
用法
將不同層級的物體放入不同的父物體下分別管理。

給每個父物體掛上腳本。

Follow為跟隨的基準(zhǔn)對象。(比如玩家,相機等)
ScaleOffset為移動速率,1為和目標(biāo)移速一致,越小越慢,越大越快。0為不移動,負值為反向移動。(前景可能要用到負值)
Hor和Ver為跟隨哪個軸。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題
這篇文章主要介紹了C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題 的相關(guān)資料,需要的朋友可以參考下2016-03-03
C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實例形式簡單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實現(xiàn)技巧,需要的朋友可以參考下2017-02-02

