问题描述
我正在研究简单的3D手机游戏.在游戏结束时,我想用简单的增/减分数值创建面板.
I'm working on simple 3d mobile game. At the end of gameplay I would like to create Panel with simple increasing/decreasing score values.
我不断增加的代码基于以下主题的@empty答案: Unity3D :加快排行榜计数
I'm base my increasing code on @empty answer from topic: Unity3D: Speed up highscore count up
我遇到的问题是,当我以最基本的方式运行此脚本时(在无条件的Update中-它可以工作):
The problem I have is that when I run this script in most basic way (in Update with no conditions - it works):
t = Mathf.MoveTowards(t, 1.0f, Time.deltaTime/scoreAnimationLength); // Move t towards the second value
int scoreToDisplay = (int)Mathf.Lerp(0, 1000f, t);
scoreText.text = "Score " + scoreToDisplay.ToString();
但是当我将其添加到不是从一开始就处于活动状态的GameObject上时,或将其添加到类似条件下
but when I add it on GameObject that is active not from beginning or add it in condition like
if(GameManager.instance.gameOver == true)
{
//code
}
这只是行不通-我的意思是变化的乐谱文本有效,但是没有动画-它只会显示最终值.
it just doesn't work - I mean the changing score text works, but without animation - it just display final value.
我认为Time.deltaTime存在问题,试图与自己的deltaTime一起使用
I believe that problem is with Time.deltaTime, tried to work with my own deltaTime
myDeltaTime = Time.time - lastUpdate;
lastUpdate = Time.time;
但它不起作用.
如何在延迟的条件下制作此动画?
How I can make working this animation in delayed conditions?
推荐答案
您的条件为if(GameManager.instance.gameOver == true)
,因此当游戏结束时,它将执行代码并更新值.
Your condition says if(GameManager.instance.gameOver == true)
, so when the game is over, it executes the code and update the value.
因此,您要做的是,在某些更新或常规例程中,您必须增加score的值,最佳选择将是常规例程.不要使用线程(个人意见).在另一种更新方法中,您只需编写scoreText.text = "Score " + scoreToDisplay.ToString();
.
So what you have to do is, inside some update or co-routine you have to increase the value of score, best choice would be co-routine. Don't use thread(personal opinion). And in another update method you can just write scoreText.text = "Score " + scoreToDisplay.ToString();
.
回到协程中,当if(GameManager.instance.gameOver == true)
为真时,只需销毁协程即可.
Back in the co-routine, when if(GameManager.instance.gameOver == true)
gives you true, just destroy the co-routine.
这篇关于随时间增加分数值-Unity C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!