MrMee
MrMee

Reputation: 153

unity2d: Character moves in different speeds on different phones...(Deltatime?)

so I have this little programm where my player bounces back and forth. His moving left looks like this:

if  ((runright == false) && (birdyDead == false)) 
{
    float currentposition = 0;
    currentposition = transform.position.x;

    Vector3 position = this.transform.position;
    position.x = currentposition - movespeed;
    this.transform.position = position;
    lookleft = true;            
}

I have noticed however, that depending on the phone that I am debugging my game on, the player moves with different speeds. In some extreme cases he is just WAY TOO QUICK. like uncontrollable quick... I read that maybe I need to somehow multiply some value with time.deltatime which does something to the framerate so that no matter what phone i am using my player is always on the same speed. Im guessing I need to do this to my jump function too, right?:

if (((Input.GetMouseButtonDown(0)) && (didjump == false) && (birdyDead == false)))
{
    GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpPower), ForceMode2D.Impulse);
    didjump = true;
    anim.SetBool("Jump", true);
}

Can you help me and show me what do to so that my player still moves the speed that I want it to, but delta time is in the game as well? Thanks :)

Upvotes: 1

Views: 313

Answers (1)

Galandil
Galandil

Reputation: 4249

You just need to multiply movespeed * Time.deltaTime, in order to update the position using a velocity of distance per seconds instead of distance per frame.

Upvotes: 5

Related Questions