- Home /
When raising object/s to one direction how can I calculate the last position of the last scaled part of the object/s ?
This is how I'm scaling to one direction :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangePivotDirections : MonoBehaviour
{
public enum Directions { Left, Right, Up, Down, Both_Left_Right, Both_Up_Down };
public Directions direction;
public bool scaleBothDirections = false;
public Transform prefabToScale;
public float scalingamount;
public float scalingDuration;
public string directionState;
private Vector3 ToScale;
private bool isScaling = false;
private float setPivot;
private void Start()
{
ToScale = prefabToScale.transform.localScale;
if (scaleBothDirections == false)
{
ScalingDirections();
ToScaling();
}
else
{
ScalingDirections();
}
}
IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
//Make sure there is only one instance of this function running
if (isScaling)
{
yield break; ///exit if this is still running
}
isScaling = true;
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
isScaling = false;
}
private void ScalingDirections()
{
var child = prefabToScale.GetChild(0).transform;
if (scaleBothDirections == false)
{
if (direction == Directions.Left)
{
setPivot = prefabToScale.localScale.x / 2;
prefabToScale.localPosition = new Vector3(prefabToScale.localPosition.x + setPivot, prefabToScale.localPosition.y, prefabToScale.localPosition.z);
child.localPosition = new Vector3(child.localPosition.x - setPivot, child.localPosition.y, child.localPosition.z);
}
if (direction == Directions.Right)
{
setPivot = prefabToScale.localScale.x / 2;
prefabToScale.localPosition = new Vector3(prefabToScale.localPosition.x - setPivot, prefabToScale.localPosition.y, prefabToScale.localPosition.z);
child.localPosition = new Vector3(child.localPosition.x + setPivot, child.localPosition.y, child.localPosition.z);
}
if (direction == Directions.Up)
{
setPivot = prefabToScale.localScale.y / 2;
prefabToScale.localPosition = new Vector3(prefabToScale.localPosition.x, prefabToScale.localPosition.y - setPivot, prefabToScale.localPosition.z);
child.localPosition = new Vector3(child.localPosition.x, child.localPosition.y + setPivot, child.localPosition.z);
}
if (direction == Directions.Down)
{
setPivot = prefabToScale.localScale.y / 2;
prefabToScale.localPosition = new Vector3(prefabToScale.localPosition.x, prefabToScale.localPosition.y + setPivot, prefabToScale.localPosition.z);
child.localPosition = new Vector3(child.localPosition.x, child.localPosition.y - setPivot, child.localPosition.z);
}
}
else
{
if (direction == Directions.Both_Left_Right)
{
StartCoroutine(scaleOverTime(prefabToScale, new Vector3(ToScale.x + scalingamount, ToScale.y, ToScale.z), scalingDuration));
}
if (direction == Directions.Both_Up_Down)
{
StartCoroutine(scaleOverTime(prefabToScale, new Vector3(ToScale.x, ToScale.y + scalingamount, ToScale.z), scalingDuration));
}
}
directionState = direction.ToString();
}
private void ToScaling()
{
if (direction == Directions.Left || direction == Directions.Right)
{
StartCoroutine(scaleOverTime(prefabToScale, new Vector3(scalingamount + setPivot, ToScale.y, ToScale.z), scalingDuration));
}
if (direction == Directions.Up || direction == Directions.Down)
{
StartCoroutine(scaleOverTime(prefabToScale, new Vector3(ToScale.x, scalingamount, ToScale.z), scalingDuration));
}
}
private void Update()
{
}
}
For example this is a screenshot after scaled object to the right : The red circle show the end scaling on the right side.
Now I have more prefabs in the hierarchy the first is scaling left the next one is scaling down. I want somehow when running the game that the next prefab in the hierarchy will start scaling down from the first prefab last scaled position.
I mean that the next prefab will start scaling down from the red circle position. And the next prefab will scale left or any direction from the end of the second prefab and so on.
Each next prefab to scale from the last scaled position the end of the prefab before.
So I started a new script ScalingManager : But not sure how to make the calculations and how to and where to position the prefabs. All prefabs will start scaling the same time but each next prefab should start from the end position of the prefab before.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScalingManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var ObjectsToScale = GameObject.FindGameObjectsWithTag("Scaling Object");
}
// Update is called once per frame
void Update()
{
}
}
ObjectsToScale find all the prefabs.
Here is example of what I mean that the next prefab will start scaling from the end position of the prefab before :
The rule should be that each prefab will start scaling position will be from the end scaled position of the prefab before.