2017-12-22 12 views
0

私がしたいことは、最初のスクリプトでFを一度押してオブジェクトを表示し、コルーチンを開始して2番目のオブジェクトのオブジェクトのスケールを変更することです。なぜオブジェクトのスケーリングが常にノンストップで上下に変化していますか?

問題は、オブジェクトをノンストップで上下にスケーリングしていて、それを一度スケールしたいのですが、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(); 
      } 
     } 
    } 
} 

オブジェクトスケール2番目のスクリプト:私は自分自身をそれをテストすることはできませんが

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()); 
    } 
} 
+2

'while(true)' = "永遠に繰り返す" – Draco18s

答えて

1

を、私はいくつかのマイナーな変更を行いました私が信じるのは、あなたに望ましい行動を与えるはずです。最初のスクリプトは次のようになる。

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(); 
     } 
    } 
} 

第二のスクリプトは次のようになる。

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()); 
    } 
} 

私は基本的に2つの異なるルーチンを作りました。 1つはスケールアップし、もう1つはスケールダウンします。 Fキーを押すと、オブジェクトのアクティブな状態に適応し、その状態に応じて右コルーチンが呼び出されます。

+1

コードを投稿するだけではありません。あなたの答えを説明してください。あなたが変更したことや問題が何であるかをOPに伝えます。 – Programmer

+1

私は小さな説明を入力していた、謝罪しました。 –

+0

@GlennCodesありがとうございます。 ScaleUpパーツは、ScaleDownパーツがうまくいき、ScaleUpのようにゆっくりとスケールダウンしません。ScaleDownはオブジェクトを一度に消してしまいます。 –

関連する問題