I have an array of cubes, all spawned at different heights.
Currently I have it so when the up button is pressed it transform them all to z = 0, keeping them in their current location on the x, y axis.
Here is my current code:
using UnityEngine;
using System.Collections;
public class zeroObjects : MonoBehaviour {
public bool move;
public float duration = 0.5f;
private float t;
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
move = true ;
}
if (move)
{
double TLoc;
TLoc = transform.position.y;
t += Time.deltaTime / duration;
Vector3 posA = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Vector3 posB = new Vector3(transform.position.x, 0, transform.position.z);
if(TLoc > 0){
print(posA);
transform.position = Vector3.Lerp(posA, posB, t);
}
if(TLoc == 0){
move = false;
}
}
}
}
It works.
But I am relatively new to unity, and have just started learning C#.
Is there a better way to achieve what I am doing? Or am I on the right track?