C# 随时间缩放游戏对象
我在unity中制作了一个测试游戏,当我点击一个按钮时,它会生成一个从工厂类创建的圆柱体。我试着让它成为这样,当我创建圆柱体时,它的高度在接下来的20秒内收缩。我发现有些方法很难转化为我正在做的事情。如果你能把我引向正确的方向,我将非常感激 这是我的圆柱体类代码C# 随时间缩放游戏对象,c#,unity3d,C#,Unity3d,我在unity中制作了一个测试游戏,当我点击一个按钮时,它会生成一个从工厂类创建的圆柱体。我试着让它成为这样,当我创建圆柱体时,它的高度在接下来的20秒内收缩。我发现有些方法很难转化为我正在做的事情。如果你能把我引向正确的方向,我将非常感激 这是我的圆柱体类代码 public class Cylinder : Shape { public Cylinder() { GameObject cylinder = GameObject.CreatePrimitive(Prim
public class Cylinder : Shape
{
public Cylinder()
{
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(3, 0, 0);
cylinder.transform.localScale = new Vector3(1.0f, Random.Range(1, 2)-1*Time.deltaTime, 1.0f);
cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
Destroy(cylinder, 30.0f);
}
}
公共类圆柱体:形状
{
公共缸()
{
GameObject圆柱体=GameObject.CreatePrimitive(PrimitiveType.cylinder);
圆柱体.transform.position=新矢量3(3,0,0);
cylinder.transform.localScale=新矢量3(1.0f,随机范围(1,2)-1*Time.deltaTime,1.0f);
圆柱体.GetComponent().material.color=Random.ColorHSV();
破坏(气缸,30.0f);
}
}
退房。如何使用它的一般示例如下:
float t = 0;
Update()
{
t += Time.deltaTime;
cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}
public class ShrinkBehaviour : MonoBehaviour
{
bool isNeedToShrink;
Config currentConfig;
float startTime;
float totalDistance;
public void StartShrink(Config config)
{
startTime = Time.time;
currentConfig = config;
totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
isNeedToShrink = true;
transform.localScale = config.startSize;
}
private void Update()
{
if (isNeedToShrink)
{
var nextSize = GetNextSize(currentConfig);
if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
{
isNeedToShrink = false;
return;
}
transform.localScale = nextSize;
}
}
Vector3 GetNextSize(Config config)
{
float timeCovered = (Time.time - startTime) / config.duration;
var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
return result;
}
public struct Config
{
public float duration;
public Vector3 startSize;
public Vector3 destinationSize;
}
}
退房。如何使用它的一般示例如下:
float t = 0;
Update()
{
t += Time.deltaTime;
cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}
public class ShrinkBehaviour : MonoBehaviour
{
bool isNeedToShrink;
Config currentConfig;
float startTime;
float totalDistance;
public void StartShrink(Config config)
{
startTime = Time.time;
currentConfig = config;
totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
isNeedToShrink = true;
transform.localScale = config.startSize;
}
private void Update()
{
if (isNeedToShrink)
{
var nextSize = GetNextSize(currentConfig);
if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
{
isNeedToShrink = false;
return;
}
transform.localScale = nextSize;
}
}
Vector3 GetNextSize(Config config)
{
float timeCovered = (Time.time - startTime) / config.duration;
var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
return result;
}
public struct Config
{
public float duration;
public Vector3 startSize;
public Vector3 destinationSize;
}
}
这可以通过协程函数中的
Time.deltaTime
和Vector3.Lerp
实现。类似于和问题。对它进行了一点修改以实现此目的
bool isScaling = false;
IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
//Make sure there is only one instance of this function running
if (isScaling)
{
yield break; ///exit if this is still running
}
isScaling = true;
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
isScaling = false;
}
这可以通过协程函数中的
Time.deltaTime
和Vector3.Lerp
实现。类似于和问题。对它进行了一点修改以实现此目的
bool isScaling = false;
IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
//Make sure there is only one instance of this function running
if (isScaling)
{
yield break; ///exit if this is still running
}
isScaling = true;
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
isScaling = false;
}
您将创建一个新的MonoBehavior脚本并将其添加到原语中。然后您将使用monobhavior的“更新”方法(或使用协同程序)来随时间改变对象 单一行为必须如下所示:
float t = 0;
Update()
{
t += Time.deltaTime;
cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}
public class ShrinkBehaviour : MonoBehaviour
{
bool isNeedToShrink;
Config currentConfig;
float startTime;
float totalDistance;
public void StartShrink(Config config)
{
startTime = Time.time;
currentConfig = config;
totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
isNeedToShrink = true;
transform.localScale = config.startSize;
}
private void Update()
{
if (isNeedToShrink)
{
var nextSize = GetNextSize(currentConfig);
if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
{
isNeedToShrink = false;
return;
}
transform.localScale = nextSize;
}
}
Vector3 GetNextSize(Config config)
{
float timeCovered = (Time.time - startTime) / config.duration;
var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
return result;
}
public struct Config
{
public float duration;
public Vector3 startSize;
public Vector3 destinationSize;
}
}
公共类收缩行为:单行为
{
布尔需要去滑冰;
配置当前配置;
浮动起始时间;
浮动总距离;
public void StartShrink(配置)
{
startTime=Time.Time;
currentConfig=config;
totalDistance=Vector3.Distance(currentConfig.startSize,currentConfig.destinationSize);
isNeedToShrink=true;
transform.localScale=config.startSize;
}
私有void更新()
{
如果(是否需要滑冰)
{
var nextSize=GetNextSize(currentConfig);
如果(Vector3.Distance(nextSize,currentConfig.destinationSize)您将创建一个新的MonoBehavior脚本并将其添加到您的原语中。然后您将使用MonoBehavior的“更新”方法(或使用协同程序)随时间更改对象
单一行为必须如下所示:
float t = 0;
Update()
{
t += Time.deltaTime;
cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}
public class ShrinkBehaviour : MonoBehaviour
{
bool isNeedToShrink;
Config currentConfig;
float startTime;
float totalDistance;
public void StartShrink(Config config)
{
startTime = Time.time;
currentConfig = config;
totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
isNeedToShrink = true;
transform.localScale = config.startSize;
}
private void Update()
{
if (isNeedToShrink)
{
var nextSize = GetNextSize(currentConfig);
if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
{
isNeedToShrink = false;
return;
}
transform.localScale = nextSize;
}
}
Vector3 GetNextSize(Config config)
{
float timeCovered = (Time.time - startTime) / config.duration;
var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
return result;
}
public struct Config
{
public float duration;
public Vector3 startSize;
public Vector3 destinationSize;
}
}
公共类收缩行为:单行为
{
布尔需要去滑冰;
配置当前配置;
浮动起始时间;
浮动总距离;
public void StartShrink(配置)
{
startTime=Time.Time;
currentConfig=config;
totalDistance=Vector3.Distance(currentConfig.startSize,currentConfig.destinationSize);
isNeedToShrink=true;
transform.localScale=config.startSize;
}
私有void更新()
{
如果(是否需要滑冰)
{
var nextSize=GetNextSize(currentConfig);
if(Vector3.Distance(nextSize,currentConfig.destinationSize)如果我只想将对象高度缩小20秒怎么办?你没有声明速度的定义,速度是从哪里来的?我修复了它。只需按持续时间替换速度。我正在尝试,只需几秒钟就可以缩小到目标大小。已经修复了。你不需要分形。尝试再复制一次:)如果我只想将对象高度缩小20秒怎么办?你没有声明速度的定义,速度是从哪里来的?我修复了它。只需按持续时间替换速度。我正在尝试,只需几秒钟就可以缩小到目标大小。已经修复了。你不需要分形。尝试再复制一次:)Vector3 startScaleSize=objectToScale.position;
应该是Vector3 startScaleSize=objectToScale.localScale;
@TheSkimek-Yup,没错。这是一个快速端口。感谢您的更正。Vector3 startScaleSize=objectToScale.position;
应该是Vector3 startScaleSize=objectToScale.localScale
@TheSkimek是的,没错。这是一个快速端口。谢谢您的更正。