0

basically my code is working fine and all is good. My only problem is that the character is changing position is splits of seconds from this line

float perc = currentLerpTime / lerpTime;
        var _CurrentPosition = gameObject.transform.position.x;
        var DesiredPosition = Mathf.Lerp (_CurrentPosition, _CurrentPosition + evadeForce, perc);
        gameObject.transform.position = new Vector2 (DesiredPosition, gameObject.transform.position.y);

the code above gets current position then lerps to new position then applies it. but it is happening too fast like the player is disappearing. instead i want it to be slower.

basically instead of going from A to B in 0.1 seconds I want it to go from A to B in 1 second.

Here is the full code:

IEnumerator _EvadeAttacks() 
{
    Physics2D.IgnoreLayerCollision(playerLayer, EnemyLayer, true);
    currentLerpTime = 0f;
    currentLerpTime += Time.deltaTime;
    if (currentLerpTime > lerpTime) {
        currentLerpTime = lerpTime;
}
    if (!evadeLeft) 
    {
        Debug.Log ("workingright");
        float perc = currentLerpTime / lerpTime;
        var _CurrentPosition = gameObject.transform.position.x;
        var DesiredPosition = Mathf.Lerp (_CurrentPosition, _CurrentPosition + evadeForce, perc);
        gameObject.transform.position = new Vector2 (DesiredPosition, gameObject.transform.position.y);
        hasEvaded = true;
    } else if (evadeLeft) 
    {
        Debug.Log ("workingleft");
        float perc = currentLerpTime / lerpTime;
        var _CurrentPosition = gameObject.transform.position.x;
        var DesiredPosition = Mathf.Lerp (_CurrentPosition, _CurrentPosition - evadeForce, perc);
        gameObject.transform.position = new Vector2 (DesiredPosition, gameObject.transform.position.y);
        hasEvaded = true;
    }
    canWalk = true;
    yield return new WaitForSeconds(SecondsToWaitForEvadeCollider);
    Physics2D.IgnoreLayerCollision(playerLayer, EnemyLayer, false);
    yield return new WaitForSeconds (SecondsToWaitForEvadeReset);
    hasEvaded = false;
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Zyzz Shembesh
  • 220
  • 3
  • 19

1 Answers1

0

Since you change only X i will create function for only X change. If you want to change both, just modify it.

public void MoveGameObject(GameObject go, double Y,  double currentX, double desiredX, double moveSpeed)
{
    double K = moveSpeed * 0.1; //Try different moveSpeed for effect
    double D = (desiredX - currentX) / K;
    double X = currentX;

    for(int I = 0; I < K; I++)
    {
        X += D;
        go.transform.position = new Vector2 (X, Y);
    }
}

If there is mistake (I have done it from head without testing), tell me what it is and I will correct it.

Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54