第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何移動(dòng)一個(gè)精靈,將其更改為另一個(gè)精靈,然后再次移動(dòng)它?

如何移動(dòng)一個(gè)精靈,將其更改為另一個(gè)精靈,然后再次移動(dòng)它?

C#
不負(fù)相思意 2023-07-09 14:58:30
我試圖將一個(gè)精靈保持在屏幕中央幾秒鐘,然后將其平滑地向左移動(dòng),直到它離開(kāi)屏幕,然后我想要一個(gè)不同的精靈,它應(yīng)該從屏幕右側(cè)移動(dòng)到屏幕右側(cè)屏幕中心;所有這一切都在無(wú)限循環(huán)中。一個(gè)非常清楚的例子:public Sprite Pokemon_0;public Sprite Pokemon_1;? ? void Start()? ? {? ? GetComponent<SpriteRenderer>().sprite = Pokemon_0;}? ? void Update()? ? {? ? if (transform.position.x >= -80)?? ? {? ? ? ? transform.Translate(-1f,0f,0f);? ? }? ? else? ? {? ? ? ? GetComponent<SpriteRenderer>().sprite = Pokemon_1;? ? ? ? ?? ? }以下是據(jù)我所知。它將精靈平滑地向左移動(dòng),直到它離開(kāi)屏幕,然后將精靈更改為其他精靈。我很新,所以如果有人可以編寫一個(gè)簡(jiǎn)單的腳本,那將會(huì)非常有幫助。我也在努力學(xué)習(xí),因此將不勝感激。
查看完整描述

1 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

// The [Range()] attribute will make `speed` show up with a slider in the inspector


[Range(0.1f, 5)]

public float speed = 3;

public Sprite[] Pokemons;


private SpriteRenderer sr;

private float worldMiddleX;

private float worldLeftX;

private float worldRightX;

private int currentPokemon = 0;

private float secondsInMiddle = 3f;



void Start()

{

? ? sr = GetComponent<SpriteRenderer>();

? ? sr.sprite = Pokemons[currentPokemon];


? ? // Find out what X's in the world the left edge, middle & right of the screen is,

? ? // if you have a 1920x1080 resolution Screen.width will be 1920. But we don't

? ? // want to position us at x = 1920, because that is FAR away

? ? // So we need to figure out where in the GAME world, our screen right edge is

? ? worldMiddleX = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width / 2, 0f)).x;


? ? // +100 & -100 because we want it to go outside of the screen a little bit

? ? // These should be tweaked based on the sprite width

? ? // Can be done dynamically / programatically too. Their width is in

? ? // Pokemons[0].rect.width

? ? worldLeftX = Camera.main.ScreenToWorldPoint(new Vector2(-100, 0f)).x;

? ? worldRightX = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width + 100, 0f)).x;


? ? // Start our endlessly looping Coroutine, which will take care of?

? ? // 1. Moving to the middle.

? ? // 2. Waiting in middle

? ? // 3. Moving outside of screen to the left & resetting position

? ? // 4. Repeat

? ? StartCoroutine(SlideLeftButWaitMiddleEndlesslyLoop());

}


/*

* Changed from void to IEnumerator to get access to the

* `yield` keyword, which will let us pause execution for X seconds.

* If you do this with a normal method, you will need to invoke it with

* `StartCoroutine()` as it becomes a Coroutine, which will execute in?

* an asynchronous manner

*/

IEnumerator SlideLeftButWaitMiddleEndlesslyLoop()

{

? ? while (true)

? ? {

? ? ? ? // yield return *Coroutine* will make sure that the code doesn't continue?

? ? ? ? // its code path until it has completed its Coroutine (method execution)


? ? ? ? // Wait for MoveToMiddle() to finish executing

? ? ? ? yield return StartCoroutine(MoveToMiddle());


? ? ? ? // Wait for x seconds

? ? ? ? yield return new WaitForSeconds(secondsInMiddle);


? ? ? ? // Wait for MoveOutsideScreenAndReset() to finish executing

? ? ? ? yield return StartCoroutine(MoveOutsideScreenAndReset());

? ? }

}


IEnumerator MoveToMiddle()

{

? ? while (transform.position.x > worldMiddleX)

? ? {

? ? ? ? transform.Translate(-(speed * Time.deltaTime), 0f, 0f);

? ? ? ? yield return null;


? ? ? ? // yield return null here will make this loop pause until next frame

? ? ? ? // so that it doesn't just do all the loops instantly and teleport your sprite

? ? }

}


IEnumerator MoveOutsideScreenAndReset()

{

? ? while (transform.position.x > worldLeftX)

? ? {

? ? ? ? transform.Translate(-(speed * Time.deltaTime), 0f, 0f);

? ? ? ? yield return null;


? ? ? ? // yield return null here will make this loop pause until next frame

? ? ? ? // so that it doesn't just do all the loops instantly and teleport your sprite

? ? }


? ? // Change to the next sprite

? ? nextPokemonSprite();


? ? // Reset the position, then the Update() method will start again from the top

? ? // since this method will have fully executed

? ? Vector3 pos = transform.position;

? ? pos.x = worldRightX;

? ? transform.position = pos;

}


// If we're at the last Pokemon in the Pokemons array, start over at 0

void nextPokemonSprite()

{

? ? currentPokemon++;

? ? if (currentPokemon == Pokemons.Length)

? ? ? ? currentPokemon = 0;


? ? sr.sprite = Pokemons[currentPokemon];

}

查看完整回答
反對(duì) 回復(fù) 2023-07-09
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)