I have two scripts and two problems.
The first script is when i press on F once it’s showing the object. Pressing F again will not show the object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroidMove : MonoBehaviour
{
public GameObject droid;
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);
}
}
}
The second script should scale the object slowly once from 0 to 1:
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 = 7;
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);
}
}
}
private void Update()
{
if (objectToScale.activeInHierarchy == true)
{
StartCoroutine(Breath());
}
}
}
The problems are in the ChangeScale script.
First problem is that i want to scale the object once from o to 1 (from 0,0,0 to 1,1,1) so i’m checking in the Update if the droid is active in hierarchy when i press on F then start the coroutine:
private void Update()
{
if (objectToScale.activeInHierarchy == true)
{
StartCoroutine(Breath());
}
}
But since it’s all the time true it keep changing the object scale non stop.
The second problem is changing the scaling time. I tried to change the variable AnimationTimeSeconds to 7 but it didn’t change much so i changed the FramesCount to 1000 so the first time the scaling was a bit slowly but then each time the scaling is getting faster and faster.
What i want is two things:
To change the scaling once. And to be able to control the scaling time.