Cant jump while moving left or right

I cant move while jumping left or right and i dont know how to fix it

Here is my code:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Playermovement : MonoBehaviour
{
    [SerializeField] GameObject Deathcontroll;
    [SerializeField] Rigidbody2D rb;
    [SerializeField] float jumppower;
    [SerializeField] bool Canjump;
    [SerializeField] float movepower;
    [SerializeField] bool alive;
    [SerializeField] bool Dpress;
    [SerializeField] bool Apress;
    private logiccontroller controller;
    private bool spacedown;

    // Start is called before the first frame update

    private void Awake()
    {
       
    }

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        alive = true;
        controller = Deathcontroll.GetComponent<logiccontroller>();
    }
   
    // Update is called once per frame
    void Update()
    {
        if (controller.isdead == true)
        {
            alive = false;
        }
        else
        {
            alive = true;
        }


        if (Input.GetKey(KeyCode.D))
        {
            Dpress = true;  
        }
        else
        {
            Dpress = false;
        }

        if (Input.GetKey(KeyCode.A))
        {
            Apress = true;
        }
        else
        {
            Apress = false;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            spacedown = true;
        }
        else
        {
            spacedown = false;
        }

        if (Canjump == true && spacedown == true && alive == true)
        {
            rb.velocity = Vector2.up * jumppower;
            controller.jumped = true;
        }
        else
        {
            controller.jumped = false;
        }

        if (Dpress == true && alive == true)
        {
            rb.velocity = Vector2.right * movepower;
        }

        if (Apress == true && alive == true)
        {
            rb.velocity = Vector2.left * movepower;
        }

        if (controller.reset == true)
        {
            gameObject.transform.position = new Vector3(0 , 0, 0);
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "grass")
        {
            Canjump = true;
            Debug.Log("coldide");
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "grass")
        {
            Canjump = false;
            Debug.Log("not colided");
        }
    }


}

Start by clearing the code, it’s long and hard to read.

// Long
if (controller.isdead == true)
{
    alive = false;
}
else
{
    alive = true;
}

// Short
alive = !controller.isdead;
// Long
if (Input.GetKey(KeyCode.D))
{
    Dpress = true;
}
else
{
    Dpress = false;
}

if (Input.GetKey(KeyCode.A))
{
    Apress = true;
}
else
{
    Apress = false;
}

if (Input.GetKeyDown(KeyCode.Space))
{
    spacedown = true;
}
else
{
    spacedown = false;
}

// Short
Dpress = Input.GetKey(KeyCode.D);
Apress = Input.GetKey(KeyCode.A);
spacedown = Input.GetKeyDown(KeyCode.Space);
// Long
if (Dpress == true && alive == true)
{
    rb.velocity = Vector2.right * movepower;
}

if (Apress == true && alive == true)
{
    rb.velocity = Vector2.left * movepower;
}

// Short
if ((Dpress || Apress) && alive)
{
    rb.velocity = new Vector2((Dpress ? 1 : -1) * movepower, rb.velocity.y);
}

Edit: The final code can look like this. Try it out, maybe it solved the problem.

using UnityEngine;

public class Playermovement : MonoBehaviour
{
    [SerializeField] GameObject Deathcontroll;
    [SerializeField] Rigidbody2D rb;
    [SerializeField] float jumppower;
    [SerializeField] float movepower;
    private logiccontroller controller;
    private bool alive;
    private bool spacedown;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        alive = true;
        controller = Deathcontroll.GetComponent<logiccontroller>();
    }

    private void Update()
    {
        alive = !controller.isdead;

        bool Dpress = Input.GetKey(KeyCode.D);
        bool Apress = Input.GetKey(KeyCode.A);
        spacedown = Input.GetKeyDown(KeyCode.Space);

        if (CanJump() && spacedown && alive)
        {
            rb.velocity = Vector2.up * jumppower;
            controller.jumped = true;
        }
        else
        {
            controller.jumped = false;
        }

        if ((Dpress || Apress) && alive)
        {
            rb.velocity = new Vector2((Dpress ? 1 : -1) * movepower, rb.velocity.y);
        }

        if (controller.reset)
        {
            transform.position = Vector3.zero;
        }
    }

    private bool CanJump()
    {
        return Canjump && grounded;
    }

    private bool grounded;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        grounded = collision.gameObject.tag == "grass";
        if (grounded)
        {
            Debug.Log("collided");
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "grass")
        {
            grounded = false;
            Debug.Log("not collided");
        }
    }
}

It fixed my original problem but now i cant jump without enabling canjump manually

And btw the reason my code is so long is becouse i am new to programming

Fixed it just had to remove canjump from Return canjump && grounded

Everyone had to start so it’s fine. Now you can notice where you can write something shorter, especially working with booleans. Try to follow this https://learn.unity.com/pathway/junior-programmer . This will give you extra basics in most common problems. In Unit 2 I remember it was about jumping and grounding, and how to animate.

You are right I had to skip something, it’s good you already fixed this.