0

So my movement isn't instant. You accelerate and decelerate. I use two floats starting at 0, going to 1, to calculate my movement speed. Here it is:

private void Update()
{   
    float moveTowardsX = 0;
    float moveTowardsY = 0;

    // Adjust thrust to be below max Thrust
    if(thrust >= maxThrust)
    {
        thrust = maxThrust;
    }

    // Calculates movement speed based on all variables
    float changeRatePerSecond = 1 / timeFromZeroToMax * thrust * Time.deltaTime;
    changeRatePerSecond /= weight / 1.5f;

    if(onMobile == true)
    {
        // Checks for Input. If Input is detected, values get changed [MOBILE]
        if(isMovingLeft == true && isMovingRight == false)
        {
            moveTowardsX = -1.0f;
        }
        else if(isMovingRight == true && isMovingLeft == false)
        {
            moveTowardsX = 1.0f;
        }

        if(isMovingUp == true && isMovingDown == false)
        {
            moveTowardsY = 1.0f;
        }
        else if(isMovingDown == true && isMovingUp == false)
        {
            moveTowardsY = -1.0f;
        }

        if(isRotatingLeft == true && isRotatingRight == false)
        {
            rotationsPerSecond++;
        }
        else if(isRotatingRight == true && isRotatingLeft == false)
        {
            rotationsPerSecond--;
        }
    }

Then I parse these values into my movement code and smooth them out:

void FixedUpdate()
{
    // Makes values change smoothly
    valueX = Mathf.MoveTowards(valueX, moveTowardsX, changeRatePerSecond);
    valueY = Mathf.MoveTowards(valueY, moveTowardsY, changeRatePerSecond);

    // Turn numbers into movement
    rb.velocity = new Vector2(valueX * runSpeed, valueY * runSpeed);
}

The Problem is that when I run into a wall, the numbers don't decrease. And if I run into a wall standing still, the numbers increase. Here is a video linking to my problem too (Look at the top right): https://youtu.be/XRE9p0yo4GA

What could I implement to fix this?

1 Answers1

0

I have 2 suggestions, either one should work:

  1. Do a ground check. Platformers often perform ground checks to check that the player is on the ground. By doing a box cast, you can check if your character is adjacent to a wall in the direction of their movement, and if they are, do not let them move (or gain speed) in that direction.

Here is a video for how to do a ground check. I would recommend the box cast method here, since it would give you the most accurate results: https://www.youtube.com/watch?v=c3iEl5AwUF8

  1. Use the rb.velocity of the object in a direction divided by your character's maximum speed in place of your valueX and valueY. So, use:

rb.velocity.x / runSpeed

instead of

valueX

and the same for y. This would take into account anything that changes the velocity of the object outside this script, such as crashing into a wall, but also if your character is thrown by some effect you create later on.

Toll Gnoll
  • 160
  • 1
  • 6
  • No, I spend the last 3 days playtesting and re-writing everything. It doesnt work like that. Here is the **full** code with velocity. And I still gain momentum when running into a wall: https://pastebin.com/KxRjYCx4 – ljksfenfkljbakfkja Dec 30 '22 at 11:12
  • Sorry, should have made it clearer that when I said rb.velocity I meant using Rigidbody2D and its velocity. If you run into a wall, rb.velocity should change to about zero in that dimension (unless your object is set to bounce). Using rb.velocity and increasing that slowly should fix the issue, since if you are in contact with a wall, the velocity in the direction of the wall will become zero each frame, stopping the character from speeding up. You have created a velocity variable, but it does not seem to be synchronised with your RIgidbody2D's velocity. – Toll Gnoll Dec 31 '22 at 01:57