Home > Mobile >  GetAxisRaw isn't accepting inputs to the right
GetAxisRaw isn't accepting inputs to the right

Time:02-01

For my Computer Science A-Level Programming Project I am developing a game and I wanted to include an acceleration and momentum system akin to the Genesis Sonic games. I have created an acceleration system however, for some reason it only allows my character to move left and not right. For obvious reasons I need it to move right.

Here is my code. It is written in C#:

 float acceleration_speed = 0.46875f;
 float deceleration_speed = 5.0f;
 float friction_speed = 0.66875f;
 float top_speed = 60.0f;
 float horizontalMove = 0f;

 if (Input.GetAxisRaw("Horizontal") < 0) //if player is pressing left
        {
            if (horizontalMove > 0.0f)
            {
                horizontalMove -= deceleration_speed; //decelerate

                if (horizontalMove <= 0.0f)
                {
                    horizontalMove = -0.5f;
                }
            }

            else if (horizontalMove > -top_speed)
            {
                horizontalMove -= acceleration_speed; //accelerate
                if (horizontalMove <= -top_speed)
                {
                    horizontalMove = -top_speed; //impose top speed limit
                }
            }

        }

        if (Input.GetAxisRaw("Horizontal") > 0) //if player is pressing right
        {
            if (horizontalMove < 0.0f) //if moving to the left
            {
                horizontalMove  = deceleration_speed; //decelerate
                if (horizontalMove >= 0.0f)
                {
                    horizontalMove = 0.5f;
                }

                else if (horizontalMove < top_speed)
                {
                    horizontalMove  = acceleration_speed; //accelerate
                    if (horizontalMove >= top_speed)
                    {
                        horizontalMove = top_speed; //impose top speed limit
                    }
                }
            }
        }

        if (Input.GetAxis("Horizontal") == 0)
        {
            horizontalMove -= friction_speed; //decelerate
            if (horizontalMove <= 0)
            {
                horizontalMove = 0.0f;
            }
        }

I have used Input.GetAxisRaw to tell the program whether the player is moving left (-1) or right (1) or isn't moving at all (0).

I created the condition that if Input.GetAxisRaw < 0 (aka moving left) the system would accelerate the character in that direction, if Input.GetAxisRaw > 0 (aka moving right) the system would accelerate the character in that direction, if the character was already moving it would create friction to decelerate the character until it's speed is 0 and if the player character wasn't moving at all nothing would happen.

Now this code works when I hold the left input and do nothing but it doesn't work when I hold the right input as the player character doesn't even move.

CodePudding user response:

Compare your code of the if (Input.GetAxisRaw("Horizontal") < 0) block with the if (Input.GetAxisRaw("Horizontal") > 0) block, you'll see you misplaced the else if block in the latter. Its inside of if (horizontalMove < 0.0f) { ... } thus it will never trigger if your character is not already moving left.

        if (Input.GetAxisRaw("Horizontal") > 0) //if player is pressing right
        {
            if (horizontalMove < 0.0f) //if moving to the left
            {
                horizontalMove  = deceleration_speed; //decelerate
                if (horizontalMove >= 0.0f)
                {
                    horizontalMove = 0.5f;
                }
            }  // added
            else if (horizontalMove < top_speed)
            {
                horizontalMove  = acceleration_speed; //accelerate
                if (horizontalMove >= top_speed)
                {
                    horizontalMove = top_speed; //impose top speed limit
                }
            }
            //}  removed
        }

CodePudding user response:

You missing } in

if (Input.GetAxisRaw("Horizontal") > 0) //if player is pressing right
        {
            if (horizontalMove < 0.0f) //if moving to the left
            {
                horizontalMove  = deceleration_speed; //decelerate
                if (horizontalMove >= 0.0f)
                {
                    horizontalMove = 0.5f;
                }
//HERE YOU MISS }
                else if (horizontalMove < top_speed)
                {
                    horizontalMove  = acceleration_speed; //accelerate
                    if (horizontalMove >= top_speed)
                    {
                        horizontalMove = top_speed; //impose top speed limit
                    }
                }
            }
        }

I would suggest rewrite it so it could work with -1 and 1 input. Because you repeat the same code over again.

  • Related