Hello, This is Mahsa
I am trying to instantiate pile of money ,
So here is the problem I give them some height that they stack on top and also , I pick random spawn point , they go up every time like this →
here is the script
public MoneyData moneyPrefab;
[SerializeField] Transform spawnPoint;
[SerializeField] AnimationCurve _animationCurve;
public List<MoneyPointTf> moneyPointList = new List<MoneyPointTf>();
public List<MoneyData> moneyList = new List<MoneyData>();
public IEnumerator SpawnMoney()
{
MoneyData newMoney = Instantiate(moneyPrefab, spawnPoint.position, spawnPoint.rotation) as MoneyData;
var randPoint = Random.Range(0, moneyPointList.Count);
float timer = 0;
float duration = 5f;
var lasTileLocalPos = Vector3.zero;
if (moneyList.Count > 0)
{
lasTileLocalPos = moneyList.Last().moneyStackLocalPos;
}
var randPointTr = moneyPointList[randPoint].transform;
newMoney.moneyStackLocalPos = lasTileLocalPos + Vector3.up * newMoney.moneyHeight;
newMoney.transform.parent = randPointTr; // parent
var startScale = Vector3.zero;
var targetScale = Vector3.one;
var startPos = newMoney.transform.localPosition;
var targetPos = lasTileLocalPos; // target
newMoney.transform.localRotation = Quaternion.identity;
moneyList.Add(newMoney);
while (timer < duration)
{
timer = Mathf.Min(timer + Time.deltaTime, duration);
float t = timer / duration;
var position = Vector3.Lerp(startPos, targetPos, t);
position.y += _animationCurve.Evaluate(t);
if (duration > 0.35f)
{
newMoney.transform.localScale = Vector3.Lerp(startScale, targetScale, t);
}
newMoney.transform.localPosition = position;
yield return new WaitForEndOfFrame();
}
}