当按G键时,它将平滑缩小。 但是,如果我希望它自动按比例缩小而不按任何键,该怎么办?
我希望它会不断缩小。 使用bool标志来确定它是处于自动模式还是按键模式。
// Recommended
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
答案 0 :(得分:1)
添加布尔值“ isInAutomaticMode”吗?
private void Update()
{
if (Input.GetKeyDown(KeyCode.G) || isInAutomaticMode)
{
scaleUp = !scaleUp;
// ...
答案 1 :(得分:1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scaling : MonoBehaviour
{
public GameObject objectToScale;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
public bool scaleUp = false;
public Coroutine scaleCoroutine;
public bool automatic = false;
public bool coroutineIsRunning = false;
private void Start()
{
objectToScale.transform.localScale = minSize;
}
private void Update()
{
if(automatic)
{
if(!coroutineIsRunning)
{
Scale();
}
}
else
{
if (Input.GetKeyDown(KeyCode.G))
{
Scale();
}
}
}
private Scale()
{
scaleUp = !scaleUp;
if (scaleCoroutine != null)
StopCoroutine(scaleCoroutine);
if (scaleUp)
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
}
else
{
scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
}
}
private IEnumerator ScaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0;
Vector3 startScaleSize = targetObj.transform.localScale;
coroutineIsRunning = true;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
if(counter > duration)
coroutineIsRunning = false;
yield return null;
}
}
}