As mentioned previously, the code supposed to keep the pipes (called Top and bottom) moving while they are opening doesn´t seem to do anything but snap them right into the desired eventual position, with no animation Here's the full code of this part (Including the part that makes pipes move regularly).
System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEditor.PlayerSettings;
public class PipeMove : MonoBehaviour
{
public float deadZone= -15;
public float MoveSpeed;
public BorbPhysics borb;
public float DeathMoveSpeed = 1;
public GameObject Top;
public GameObject Bottom;
public float Offset = 5;
private Vector3 TopStartPoint;
private Vector3 TopEndPoint;
private Vector3 BottomStartPoint;
private Vector3 BottomEndPoint;
public float desiredDuration = 3f;
private float elapsedTime;
public float TxCons;
public float TyStart;
public float BxCons;
public float ByStart;
public bool IsOpening = false;
public float XOf = 4;
public float TEnd;
public float BEnd;
public bool hasSet = false;
private AnimationCurve curve;
// Start is called before the first frame update
void Start()
{
borb = GameObject.FindGameObjectWithTag("borb").GetComponent<BorbPhysics>();
TyStart = Top.transform.position.y;
ByStart = Bottom.transform.position.y;
}
// Update is called once per frame
void Update()
{
TxCons = Top.transform.position.x;
BxCons = Bottom.transform.position.x;
TopStartPoint = new Vector3(TxCons, TyStart, 0);
TopEndPoint = new Vector3(TEnd, TyStart + Offset, 0);
BottomStartPoint = new Vector3(BxCons, ByStart, 0);
BottomEndPoint = new Vector3(BEnd, ByStart - Offset, 0);
if (borb.BorbIsAlive == true)
{
transform.position = transform.position + (Vector3.left * MoveSpeed) * Time.deltaTime;
}
if (transform.position.x < deadZone)
{
Destroy(gameObject);
}
if (borb.BorbIsAlive == false)
{
transform.position = transform.position + (Vector3.left * DeathMoveSpeed) * Time.deltaTime;
}
if (IsOpening && hasSet == false)
{
TEnd = Top.transform.position.x + (-1 * MoveSpeed) * desiredDuration;
BEnd = Bottom.transform.position.x + (-1 * MoveSpeed) * desiredDuration;
hasSet = true;
}
if (IsOpening == false)
{
hasSet = false;
}
if (Input.GetKeyDown(KeyCode.W))
{
IsOpening = true;
}
if (IsOpening)
{
elapsedTime += Time.deltaTime;
float percentageComplete = elapsedTime / desiredDuration;
Top.transform.position = Vector3.Lerp(TopStartPoint, TopEndPoint, percentageComplete);
Bottom.transform.position = Vector3.Lerp(BottomStartPoint, BottomEndPoint, percentageComplete);
if (percentageComplete >= 1)
{
elapsedTime = 0f;
IsOpening = false;
}
}
}
}
I have mostly explained the issue already, but to clarify completely, the 2D game is kind of a flappy bird where you need to pick up balloons to open the pipes(I will add that later), which are obviously moving, through Vector3.Lerp. The expected result was to see the pipes smoothly open as they move along the screen. Instead, they just appear where they should eventually reach and then display the opening animation (all of this while the pipe isn´t moving, since it waits for the opening animation to end to continue moving, again, for some reason). I don`t understand why it doesnt seem to work, and I have already watched countless Lerp tutorials but none of them seem to use it when the objects in question are moving.