1
\$\begingroup\$

The first script is for the F pressing and calling the ScaleChange:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DroidMove : MonoBehaviour
{
    public GameObject droid;
    public ChangeScale changeScale;

    private bool toDisplay = false;

    private void Start()
    {
        droid.transform.localScale = new Vector3(0, 0, 0); 
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            toDisplay = !toDisplay;
            changeScale.Scale(toDisplay);
        }
    }
}

The second script make the scaling:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeScale : MonoBehaviour
{
    public GameObject objectToScale;

    private float _currentScale = InitScale;
    private const float TargetScale = 0.7f;
    private const float InitScale = 0f;
    private const int FramesCount = 10;
    private const float AnimationTimeSeconds = 0.1f;
    private float _deltaTime = AnimationTimeSeconds / FramesCount;
    private float _dx = (TargetScale - InitScale) / FramesCount;

    private IEnumerator ScaleUp()
    {
        bool 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()
    {
        bool downscaling = true;
        while (downscaling)
        {
            _currentScale -= _dx;
            if (_currentScale < InitScale)
            {
                downscaling = false;
                _currentScale = InitScale;
            }
            objectToScale.transform.localScale = Vector3.one * _currentScale;
            yield return new WaitForSeconds(_deltaTime);
        }
    }

    public void Scale(bool scaleUp)
    {
        if (scaleUp)
        {
            StartCoroutine(ScaleUp());
        }
        else
        {
            StartCoroutine(ScaleDown());
        }
    }
}

The problem is if i press on F and it start scaling up and not finished yet the scaling and i press F again instead start scaling down it's like pausing and if i keep pressing F it will keep scaling up each time a bit.

What i want is if i press F and it start scaling to one direction up.down in the middle while it's scaling then change the scaling direction.

The variable TargetScale is like max scale and InitScale is like a min. I want to keep it with the max and min but to make that if in the middle when it's scaling i press F change the scaling direction and not to pause it.

\$\endgroup\$
1
\$\begingroup\$

If I understand correctly, you want to scale the object to min or max, when F is pressed.

Try this one out:

using UnityEngine;

public class TestScaleScript : MonoBehaviour {

    private readonly Vector3 maxScale = Vector3.one * 5f;
    private readonly Vector3 minScale = Vector3.one;
    private Vector3 targetScale;
    private float scaleSpeed = 15f;

    private void Awake(){
        this.targetScale = maxScale;
    }

    private void Update(){
        this.transform.localScale = Vector3.Lerp(this.transform.localScale, targetScale, Time.deltaTime * scaleSpeed);   

        if(Input.GetKeyDown(KeyCode.F))
        {
            if(this.targetScale == maxScale) this.targetScale = minScale;
            else if(this.targetScale == minScale) this.targetScale = maxScale;
        } 
    }
}

Save it as TestScaleScript.cs in your scripts and attach it to any GameObject you want. Then, press F couple times.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.