[英]Why the scaling of the object is changing all the time nonstop up and down?
What i want to do is when i press F once on the first script and it's true to display the object then start the coroutine to change the object scale on the second object. 我想做的是在第一个脚本上按F一次,然后显示对象,然后启动协程以更改第二个对象上的对象比例,这是正确的。
The problem is that it's scaling the object non stop up and down and i want it to scale it once up and if i will press F again to not displaying the object scale it back down once. 问题是它不停地缩放对象,我希望它一次缩放,如果我再按一次F则不显示该对象就不缩小了。
The first script where i press on F: 我在F上按的第一个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroidMove : MonoBehaviour
{
public GameObject droid;
public ChangeScale changeScale;
private float distance;
private Camera cam;
private void Start()
{
cam = GetComponent<Camera>();
distance = Vector3.Distance(cam.transform.position, droid.transform.position);
droid.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
droid.SetActive(!droid.activeInHierarchy);
if (droid.activeInHierarchy == true)
{
changeScale.Scale();
}
}
}
}
The second script that scale the object: 缩放对象的第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeScale : MonoBehaviour
{
public GameObject objectToScale;
private float _currentScale = InitScale;
private const float TargetScale = 1.1f;
private const float InitScale = 0f;
private const int FramesCount = 100;
private const float AnimationTimeSeconds = 2;
private float _deltaTime = AnimationTimeSeconds / FramesCount;
private float _dx = (TargetScale - InitScale) / FramesCount;
private bool _upScale = true;
private IEnumerator Breath()
{
while (true)
{
while (_upScale)
{
_currentScale += _dx;
if (_currentScale > TargetScale)
{
_upScale = false;
_currentScale = TargetScale;
}
objectToScale.transform.localScale = Vector3.one * _currentScale;
yield return new WaitForSeconds(_deltaTime);
}
while (!_upScale)
{
_currentScale -= _dx;
if (_currentScale < InitScale)
{
_upScale = true;
_currentScale = InitScale;
}
objectToScale.transform.localScale = Vector3.one * _currentScale;
yield return new WaitForSeconds(_deltaTime);
}
}
}
public void Scale()
{
StartCoroutine(Breath());
}
}
While I can't test it out myself, I made some minor changes that I believe should give you the desired behaviour. 虽然我自己无法测试,但我进行了一些小的更改,我相信这些更改可以为您带来理想的行为。 The first script becomes:
第一个脚本变为:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroidMove : MonoBehaviour
{
public GameObject droid;
public ChangeScale changeScale;
private float distance;
private Camera cam;
private void Start()
{
cam = GetComponent<Camera>();
distance = Vector3.Distance(cam.transform.position, droid.transform.position);
droid.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
changeScale.Scale();
}
}
}
The second script becomes: 第二个脚本变为:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeScale : MonoBehaviour
{
public GameObject objectToScale;
private float _currentScale = InitScale;
private const float TargetScale = 1.1f;
private const float InitScale = 0f;
private const int FramesCount = 100;
private const float AnimationTimeSeconds = 2;
private float _deltaTime = AnimationTimeSeconds / FramesCount;
private float _dx = (TargetScale - InitScale) / FramesCount;
private bool _upScaling = false;
private bool _downScaling = false;
private IEnumerator ScaleUp()
{
_upScaling = true;
while (_upScaling)
{
_currentScale += _dx;
if (_currentScale > TargetScale)
{
_upScaling = false;
_currentScale = TargetScale;
}
objectToScale.transform.localScale = Vector3.one * _currentScale;
yield return new WaitForSeconds(_deltaTime);
}
}
private IEnumerator ScaleDown()
{
_downScaling = true;
while (_downScaling)
{
_currentScale -= _dx;
if (_currentScale < InitScale)
{
_downScaling = false;
_currentScale = InitScale;
droid.SetActive(false);
}
objectToScale.transform.localScale = Vector3.one * _currentScale;
yield return new WaitForSeconds(_deltaTime);
}
}
public void Scale(bool scaleUp)
{
if (!droid.activeInHierarchy) {
droid.SetActive(true);
StartCoroutine(ScaleUp());
}
if (_downScaling)
StartCoroutine(ScaleUp());
else
StartCoroutine(ScaleDown());
}
}
I basically made 2 different routines; 我基本上做了两个不同的例程。 one that scales up and one that scales down.
一种扩大规模,另一种缩小规模。 When you press F, I adapt the active state of the object, and depending on that state the right coroutine is called.
当您按F时,我将调整对象的活动状态,并根据该状态调用正确的协程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.