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

代碼
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;
}
}
}
用法
將不同層級(jí)的物體放入不同的父物體下分別管理。

給每個(gè)父物體掛上腳本。

Follow為跟隨的基準(zhǔn)對(duì)象。(比如玩家,相機(jī)等)
ScaleOffset為移動(dòng)速率,1為和目標(biāo)移速一致,越小越慢,越大越快。0為不移動(dòng),負(fù)值為反向移動(dòng)。(前景可能要用到負(fù)值)
Hor和Ver為跟隨哪個(gè)軸。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題
這篇文章主要介紹了C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題 的相關(guān)資料,需要的朋友可以參考下2016-03-03
C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
簡(jiǎn)單聊聊C#的線程本地存儲(chǔ)TLS到底是什么
C#的ThreadStatic是假的,因?yàn)镃#完全是由CLR(C++)承載的,言外之意C#的線程本地存儲(chǔ),用的就是用C++運(yùn)行時(shí)提供的 __declspec(thread)或__thread來虛構(gòu)的一套玩法,下面我們就來深入講講C#的線程本地存儲(chǔ)TLS到底是什么吧2024-01-01

