ok so I made this 2d platformer script, its fully functional but I’d just like to know if I can shorten it, or cut out some variables because in its current state its kinda messy and hard to navigate, also leave some opinion and useful tips for future scripts :slight_smile:

Literally copy paste:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehaviour : MonoBehaviour
{
    public KeyCode keyUp = KeyCode.W;
    public KeyCode keyLeft = KeyCode.A;
    public KeyCode keyRight = KeyCode.D;
    float movingLeft = 0;
    float movingRight = 0;

    

    float horMovement = 0f;
    
    float horMovementAcceleration = 2f;
    public float horMovementMaxSpeed = 6f;
    float horMovementDeceleration = -0.5f;
    float horFace = 1;
    
    public Rigidbody2D rb2d;
    public float jumpStrenght = 10;
    public float jumpDeceleration = 0.5f;
    float jump = 0;
    bool isGrounded = false;
    public Transform GroundCheck1; // Put the prefab of the ground here
    public LayerMask groundLayer; // Insert the layer here.


    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics2D.OverlapCircle(GroundCheck1.position, 0.15f, groundLayer);
        if (Input.GetKeyDown(keyUp) && isGrounded)
        {
            Jump();
        }

        Debug.Log("horMovement:"+horMovement);


        if (Input.GetKey(keyRight)) { movingRight = 1f; } else { movingRight = 0f; }
        if (Input.GetKey(keyLeft)) { movingLeft = 1f; } else { movingLeft = 0f; }
        
        
        //set horFace
        if ((movingLeft * -1) + movingRight != 0)
        {
            horFace = (movingLeft * -1) + movingRight;
            Debug.Log("horFace:" + horFace);
        }

        if (horMovement > .5f || horMovement < -.5f)
        {
            if (!Input.GetKey(keyRight) && !Input.GetKey(keyLeft))
            {
                Decelerate();
            }
        }
        else
        {
            horMovement = 0;
        }



        //set max speed
        if (Input.GetKey(keyRight) || Input.GetKey(keyLeft))
        {
            if (horFace == 1)
            {
                if (horMovement < horMovementMaxSpeed * 1)
                {
                    Accelerate();
                }
                else
                {
                    horMovement = horMovementMaxSpeed * 1;
                }

            }
            else
            {
                if (horMovement > horMovementMaxSpeed * -1)
                {
                    Accelerate();
                }
                else
                {
                    horMovement = horMovementMaxSpeed * -1;
                }
            }
        }




    }

    void Accelerate()
    {
        Debug.Log("Accelerating...");
        horMovement += horMovementAcceleration * horFace;
    }

    void Decelerate()
    {
        Debug.Log("Decelerating...");
        horMovement += horFace * horMovementDeceleration;
    }

    void FixedUpdate()
    {
        rb2d.velocity = new Vector2(horMovement, jump);
        if (jump >= jumpStrenght*-1) { jump -= jumpDeceleration; }
    }

    void Jump()
    {
        jump = jumpStrenght*2;
    }
}

thanks in advance :slight_smile:

I would do something like this (have not tried if it works):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehaviour : MonoBehaviour
{
    [Tooltip("The Jump key")] public KeyCode keyUp = KeyCode.W;
    [Tooltip("Walk left")] public KeyCode keyLeft = KeyCode.A;
    [Tooltip("Walk right")] public KeyCode keyRight = KeyCode.D;
    [Tooltip("How fast the character walks")] public float walkSpeed = 10;
    [Tooltip("The maximum walking speed")] public float maxWalkSpeed = 10;
    [Tooltip("How high the character jumps")] public float jumpStrenght = 10;

    public Rigidbody2D rb2d;
    bool isGrounded = false;

    public string groundTag = "ground";

    private void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == groundTag) {
            isGrounded = true;
        }
    }

    private void OnCollisionExit2D(Collision2D col)
    {
        if (col.gameObject.tag == groundTag) {
            isGrounded = false;
        }
    }

    void Jump()
    {
        rb2d.AddForce(new Vector2(0, jumpStrenght));
    }

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(keyUp) && isGrounded) {
            Jump();
        }

        float horSpeed = 0;
        if (Input.GetKey(keyRight)) { horSpeed += walkSpeed; }
        if (Input.GetKey(keyLeft)) { horSpeed -= walkSpeed; }
        rb2d.AddForce(new Vector2(horSpeed, 0));
        if(Mathf.Abs(rb2d.velocity.x) > maxWalkSpeed ) { // max velocity on x-axis
            rb2d.velocity = new Vector2( maxWalkSpeed * (Mathf.Abs(rb2d.velocity.x)/ rb2d.velocity.x), rb2d.velocity.y);
        }
    }
}