I’ve been reading all other threads on this topic and still can’t seem to find what’s causing my lerp to finish immediately.
My game is a 2D platformer, and what I’m trying to accomplish is a dash attack that can be done at any angle (controlled by the joystick). There’s an invisible child object of the player that moves in a fixed position around the player using my MoveDashReticle() function. In Update(), the player presses the Dash button, upon which the Player and DashReticle’s positions are stored as variables and used in the lerp.
So far the dash attack works at all angles, so that is good. However, I don’t want the dash to happen instantly! As you can see, I’ve used a while-loop and time.deltaTime to gradually increase the perc float of the lerp, which to my understanding should do the trick. No luck though.
I apologize if my code is a mess, as I’m only a few weeks into learning. There is more to my script but it shouldn’t be necessary to include. Any help or suggestions are greatly appreciated, thank you!
public class Move2D : MonoBehaviour
{
private GameObject player;
//Movement and Sprite Flip
Vector3 movement = Vector3.zero;
public float moveSpeed = 10f;
private bool facingLeft;
private bool facingRight;
private SpriteRenderer flip;
private SpriteRenderer starFlip;
private CapsuleCollider2D capCol;
private Rigidbody2D rb;
[SerializeField] private LayerMask groundLayerMask;
[SerializeField] private LayerMask wallLayerMask;
//Dashing
[SerializeField] private Transform dashReticle;
[Range(10.0f, 100.0f)] public float reticleDistance = 10.0f;
private bool isDashing;
[Range(0.0f, 1.0f)] public float dashTime = 1.0f;
private float currentDashTime;
Vector2 startPos;
Vector2 dashRet;
//Jumping
public float jumpForce = 450f;
public int extraJumps = 1;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
flip = GetComponent<SpriteRenderer>();
starFlip = GameObject.Find("Stars").GetComponent<SpriteRenderer>();
rb = GetComponent<Rigidbody2D>();
capCol = GetComponent<CapsuleCollider2D>();
}
void Update()
{
//Dashing
MoveDashReticle();
if (Input.GetButtonDown("Dash") && !isDashing)
{
//Get Player and Reticle Pos upon Input
startPos = player.transform.position;
dashRet = dashReticle.transform.position;
isDashing = true;
currentDashTime = 0.0f;
while (currentDashTime < dashTime && isDashing)
{
float perc = currentDashTime / dashTime;
currentDashTime += Time.deltaTime;
transform.position = Vector2.Lerp(startPos, dashRet, perc);
if (currentDashTime >= dashTime)
{
currentDashTime = dashTime;
isDashing = false;
print(isDashing);
}
}
}
}
private void MoveDashReticle()
{
Vector2 aim = new Vector2(Input.GetAxis("AimX"), Input.GetAxis("AimY"));
if (aim.magnitude > 0.0f)
{
aim.Normalize();
aim *= reticleDistance;
dashReticle.transform.localPosition = aim;
}
}
}