How to make a curve motion change direction fluidly on click
Hello folks, I have a problem with a script on a game that I am developing. It is a game where I made a curve motion algorithm for the player, but the problem is that when I double tap the player is changing direction in jagged way.
In the first picture you can see that the motion, on single tap, is like a sine wave but in the second picture you can see, on double tap, the second diris
This is the script
When player clicks we change direction
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (moveStraight && mr.alive)
moveStraight = false;
// the player is on the curve
changing = true;
ChangeDirection();
}
Reset where the curve starts in order to put the player on top of the curve
void ChangeDirection()
{
startup = 0f;
currentXPosition = transform.position.x;
direction *= -1;
}
void Move()
{
//player goes down every frame
tempPosition.y -= verticalSpeed * Time.deltaTime;
//if player has pressed the button he's not going straight anymore
if (!moveStraight)
{
if (changing)
{
//variable to follow player on the curve depending on time
startup += Time.deltaTime;
//!!!!!!!
//!!!!!!!!
//!!!!!!!!
//calculus moves player on the curve
calculus = direction * amplitude * Mathf.Sin(startup * horizontalSpeed);
//add calculus to the moving vector
tempPosition.x = calculus + currentXPosition;
//if player finished the curve keep the trajectory
if (Mathf.Sin(startup * horizontalSpeed) <= 0f)
{
changing = false;
calculus = direction * amplitude * horizontalSpeed;
}
}
else
{
tempPosition.x -= calculus * Time.deltaTime;
}
dir = transform.position - tempPosition;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
transform.position = tempPosition;
}
Can you give me a solution for this type of motion that I want?
Comment
The naive answer is "just make double-tap do what single-tap does".
People are going to need to see the code to know what is wrong.