亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何移動一個精靈,將其更改為另一個精靈,然后再次移動它?

如何移動一個精靈,將其更改為另一個精靈,然后再次移動它?

C#
不負相思意 2023-07-09 14:58:30
我試圖將一個精靈保持在屏幕中央幾秒鐘,然后將其平滑地向左移動,直到它離開屏幕,然后我想要一個不同的精靈,它應該從屏幕右側移動到屏幕右側屏幕中心;所有這一切都在無限循環中。一個非常清楚的例子: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;? ? ? ? ?? ? }以下是據我所知。它將精靈平滑地向左移動,直到它離開屏幕,然后將精靈更改為其他精靈。我很新,所以如果有人可以編寫一個簡單的腳本,那將會非常有幫助。我也在努力學習,因此將不勝感激。
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

// 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];

}

查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號