I have a movement problem

im new in unity and i tried to make a 2D character movement, but the character cant jump

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

public class NewBehaviourScript : MonoBehaviour
{
    public float speed;
    Rigidbody2D rb;
    float Xdir;


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

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Xdir = Input.GetAxis("Horizontal");
        if (Xdir > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }
        else if (Xdir < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
       
       
        if(Input.GetKeyDown(KeyCode.Space))
        {
            float JumpSpeed = 65f;
            rb.velocity = Vector2.up * JumpSpeed;
        }
    }


    void Velocity(float dir)
    {
        rb.velocity = Vector2.right * dir * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Velocity(Xdir);

       
    }


  


}

It looks to me like a simple case of you are overwriting your jump velocity every fixed update, which ofc, is where all the physics magic happens.

so what should i do?

bc when i delete the player movement the jump works

you should probably change the XDir float to a vector 3… use the Vector3.x value where you are using XDir now, and use the Vector3.y value in your jump detection check, assign the jump value to the vector3.y.

If that still doesnt make sense, i will rearrange your code a bit for you. But its really rather simple.

Yea, because you overwriting it every fixed update.

and in the function Velocity i change it too?
bc i wrote Velocity(Xdir)

o thx

i started using unity recently so i dont know a lot about coding

NP - be warned, that code will let you eternally jump. At no point are you checking to see if you are on a ground surface.

i know that was the next thing that i was gonna put

lemme try the code

it says that i cant turn unityenginevector3 into a float

did i mis something?

theres in total 5 errors

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

public class NewBehaviourScript : MonoBehaviour
{
    public float speed;
    Rigidbody2D rb;
    Vector3 Xdir;


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

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {

        Xdir = Vector3.zero;
        Xdir.y = rb.velocity.y;
        Xdir.x = Input.GetAxis("Horizontal");
        if (Xdir.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }
        else if (Xdir.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
       
       
        if(Input.GetKeyDown(KeyCode.Space))
        {
            float JumpSpeed = 65f;
            Xdir.y = JumpSpeed;
        }
    }


    void Velocity(float dir)
    {
        rb.velocity =  dir * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Velocity(Xdir);

       
    }


  


}

did i write anything wrong?

Sorry , i missed that. Change your Velocity() method to take in a Vector3.

void Velocity(Vector3 dir)
{
     ///etc...
}

JD#1539 Discord Me if you still having issues. Its just something simple and overlooked im sure.

im sorry if im being anoying butit still sshow a couple errors

At a quick glance, there was a few typos.

public class NewBehaviourScript : MonoBehaviour
    {
        public float speed;
        Rigidbody2D rb;
        Vector3 Xdir;
        //float Xdir;


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

        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            Xdir.y = rb.velocity.y;
            Xdir.x = Input.GetAxis("Horizontal");
            if (Xdir.x > 0)
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
            }
            else if (Xdir.x < 0)
            {
                transform.eulerAngles = new Vector3(0, 180, 0);
            }


            if (Input.GetKeyDown(KeyCode.Space))
            {
                float JumpSpeed = 65f;
                Xdir.y = JumpSpeed;
            }
        }
        void Velocity(Vector3 dir)
        {
            rb.velocity = dir * speed * Time.deltaTime;
        }
        private void FixedUpdate()
        {
            Velocity(Xdir);
        }
    }

Post the errors, if there is any.