Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GetAxisRaw isn't accepting inputs to the right

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading