0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Time.timeScaleの変更ではまった

Posted at

はじめに

最近Unityでゲーム作成をやり始めたので、備忘録としてはまったところを記載しようと思います。
今回はUnityで一時停止するときに使うTime.timeScaleについて記載します。

Time.timeScaleとは

上記にも書いた通り、ゲームを一時停止するときに利用する。
Time.timeScale=0にすれば、時間経過が止まりオブジェクトなどが移動しなくなる。
※ただ気を付けないといけないのは、Update()自体は止まっているわけではない。

Time.timeScale=1にすれば、時間が戻って、オブジェクトを再度動かせる。

はまった問題

Update内にて、ある条件のとき、Time.timeScale=0にして一時停止
ボタンクリック(再開ボタン)を押したときに、
Sceneを再読み込み、Time.TimeScale=1にして動きを再開させる設計とした。

Time.deltatimeが0のままで、オブジェクトが動かなくなった。

解決方法

Sceneを読み込んだあとの、オブジェクトのStart()内で、Time。timeScale=1にすればTime.deltaTimeも正常に更新されるようになった。

原因

今回、同じスクリプト(再起動ボタンのクリック処理)でSceneを読み込むことと、timeScale=0をsyした。おそらくこれだと、読み込む前のSceneでtimeScaleを更新して、新しく読み込んだSceneではその内容が反映されなかったのかと思われる。
今回は再起動目的で同じSceneを読み込む処理だったため、気付くのが遅れた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?