How to fix velocity speed for all resolutions ?

hi ,
I made a 2d game , it’s responsive for all resolutions , but the player runs slower in high resolutions , I tried addid * Time.deltaTime to velocity but it didn’t fixed it completly . I want it to have the same speed for all resolutions
it runs and jumps slower in : 1920x1080

my code :

void Start () {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        soundz = GetComponent<AudioSource> ();
        spd = GetComponent<Rigidbody2D>();

        Cloud = GameObject.Find("Cloud");
    //     rb2d.velocity *= mycam.orthographicSize * 1,
          //cloudanim = GameObject.Find("Cloud(Clone)").GetComponent<Animator>();
    }


    void OnCollisionEnter2D(Collision2D collision2D) {

        if (collision2D.relativeVelocity.magnitude > 20){
            Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
        //    cloudanim.Play("cloud");

        }
    }



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


    if (Input.GetButtonDown("Jump") && (isGrounded || !doubleJump))
        {

            soundz.Play ();

rb2d.AddForce(new Vector2(0,jumpForce));
//rb2d.velocity *= mycam.orthographicSize * 0.11f;
            if (!doubleJump && !isGrounded)
            {
                doubleJump = true;
                Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
            //    cloudanim.Play("cloud");
            }
        }
//float vert = Input.GetAxis ("Vertical") * Time.deltaTime;

    if (Input.GetButtonDown("Vertical") && !isGrounded)
        {
            rb2d.AddForce(new Vector2(0,-jumpForce));
            Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
            //cloudanim.Play("cloud");
        }
        if (isGrounded)
            doubleJump = false;


        float hor = Input.GetAxis ("Horizontal") * maxSpeed * Time.deltaTime;

        anim.SetFloat ("Speed", Mathf.Abs (hor));

        rb2d.velocity  = new Vector2 (hor * maxSpeed * Time.deltaTime, rb2d.velocity.y );

        isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F, whatIsGround);

        anim.SetBool ("IsGrounded", isGrounded);

        if ((hor > 0 && !lookingRight)||(hor < 0 && lookingRight))
            Flip ();

        anim.SetFloat ("vSpeed", spd.velocity.y);

    }




    public void Flip()
    {
        lookingRight = !lookingRight;
        Vector3 myScale = transform.localScale;
        myScale.x *= -1;
        transform.localScale = myScale;
    }
1 Like

Depends if you’re moving it in pixels, in canvas space or world space.

It looks like you might be moving it in world space. If you have an ortho camera, the vertical size of the screen is already matched to the camera in world space (read up on Camera.orthographicSize property). In other words, regardless of vertical resolution, the world distance from top to bottom will remain constant. This is how the Unity3D camera transform was chosen internal to the engine.

Obviously the lateral size of the screen will change how far the left edge is from the right. If you prefer to key your speed to this size (which may make sense for games that move mostly laterally), then you need to scale your notion of how big the screen is by the ratio (float)Screen.width / Screen.height.

2 Likes

thank you , it fixed my problem

1 Like