Skip to main content
This is C#, not the deprecated language UnityScript. Be sure to read tag info when tagging your questions.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Inconsistent jump height when rotating PCcharacter left or right

I am developing an endless runner mobile game., Similar to Subway Surfers,. I make my player character (PC) rotate slightly left or right and then return to facing forward when sliding left or right. The issue is that this rotation makes the character's jump inconsistent, resulting in different jump heights.
How

How can I make the jump consistent? Below are the scripts I am using.

In the

In PlayerControllerPlayerController script:

public void SetVelocity(Vector3 velocity)
{
    CController.Move(velocity * Time.deltaTime);

    //Rotate the player a bit where is going
    Vector3 dir = CController.velocity;
    if (dir != Vector3.zero)
    {
        dir.y = 0;
        transform.forward = Vector3.Lerp(transform.forward, dir, data.RotationSpeed * Time.deltaTime);
    }
}

In PlayerJumpingPlayerJumping script:
 

public override void Enter()
{
    player.velocityY = data.jumpForce;
}

private void UpdateJumpMovement()
{
    // Aplly gravity
    player.velocityY += data.gravity * Time.deltaTime;

    Vector3 pos = Vector3.zero;

    pos.x = player.SnapToLane();
    pos.y = player.velocityY;
    pos.z = player.currentSpeed;

    player.SetVelocity(pos);
}

Inconsistent jump height when rotating PC left or right

I am developing an endless runner mobile game. Similar to Subway Surfers, I make my player character (PC) rotate slightly left or right and then return to facing forward when sliding left or right. The issue is that this rotation makes the character's jump inconsistent, resulting in different jump heights.
How can I make the jump consistent? Below are the scripts I am using.

In PlayerController script:

public void SetVelocity(Vector3 velocity)
{
    CController.Move(velocity * Time.deltaTime);

    //Rotate the player a bit where is going
    Vector3 dir = CController.velocity;
    if (dir != Vector3.zero)
    {
        dir.y = 0;
        transform.forward = Vector3.Lerp(transform.forward, dir, data.RotationSpeed * Time.deltaTime);
    }
}

In PlayerJumping script:
 

public override void Enter()
{
    player.velocityY = data.jumpForce;
}

private void UpdateJumpMovement()
{
    // Aplly gravity
    player.velocityY += data.gravity * Time.deltaTime;

    Vector3 pos = Vector3.zero;

    pos.x = player.SnapToLane();
    pos.y = player.velocityY;
    pos.z = player.currentSpeed;

    player.SetVelocity(pos);
}

Inconsistent jump height when rotating character left or right

I am developing an endless runner mobile game, Similar to Subway Surfers. I make my player character rotate slightly left or right and then return to facing forward when sliding left or right. The issue is that this rotation makes the character's jump inconsistent, resulting in different jump heights.

How can I make the jump consistent? Below are the scripts I am using.

In the PlayerController script:

public void SetVelocity(Vector3 velocity)
{
    CController.Move(velocity * Time.deltaTime);

    //Rotate the player a bit where is going
    Vector3 dir = CController.velocity;
    if (dir != Vector3.zero)
    {
        dir.y = 0;
        transform.forward = Vector3.Lerp(transform.forward, dir, data.RotationSpeed * Time.deltaTime);
    }
}

In PlayerJumping script:

public override void Enter()
{
    player.velocityY = data.jumpForce;
}

private void UpdateJumpMovement()
{
    // Aplly gravity
    player.velocityY += data.gravity * Time.deltaTime;

    Vector3 pos = Vector3.zero;

    pos.x = player.SnapToLane();
    pos.y = player.velocityY;
    pos.z = player.currentSpeed;

    player.SetVelocity(pos);
}
Source Link
Achie1
  • 181
  • 8

Inconsistent jump height when rotating PC left or right

I am developing an endless runner mobile game. Similar to Subway Surfers, I make my player character (PC) rotate slightly left or right and then return to facing forward when sliding left or right. The issue is that this rotation makes the character's jump inconsistent, resulting in different jump heights.
How can I make the jump consistent? Below are the scripts I am using.

In PlayerController script:

public void SetVelocity(Vector3 velocity)
{
    CController.Move(velocity * Time.deltaTime);

    //Rotate the player a bit where is going
    Vector3 dir = CController.velocity;
    if (dir != Vector3.zero)
    {
        dir.y = 0;
        transform.forward = Vector3.Lerp(transform.forward, dir, data.RotationSpeed * Time.deltaTime);
    }
}

In PlayerJumping script:

public override void Enter()
{
    player.velocityY = data.jumpForce;
}

private void UpdateJumpMovement()
{
    // Aplly gravity
    player.velocityY += data.gravity * Time.deltaTime;

    Vector3 pos = Vector3.zero;

    pos.x = player.SnapToLane();
    pos.y = player.velocityY;
    pos.z = player.currentSpeed;

    player.SetVelocity(pos);
}