Search Unity

RigidBody2D movement script

Discussion in 'Getting Started' started by unitytachpro, Jun 22, 2021.

  1. unitytachpro

    unitytachpro

    Joined:
    Jun 19, 2021
    Posts:
    5
    paste the code in update function
    you need to add 2 variables
    RigidBody2D call it rb
    and
    Float call it speed


    here you can copy this code


    if (Input.GetKey(KeyCode.W))
    {
    rb.velocity = Vector3.up * speedmove;
    }
    if (Input.GetKey(KeyCode.A))
    {
    rb.velocity = Vector3.left * speedmove;
    }
    if (Input.GetKey(KeyCode.S))
    {
    rb.velocity = Vector3.down * speedmove;
    }
    if (Input.GetKey(KeyCode.D))
    {
    rb.velocity = Vector3.right * speedmove;
    }




    if (Input.GetKeyUp(KeyCode.W))
    {
    rb.velocity = Vector3.up * 0;
    }
    if (Input.GetKeyUp(KeyCode.A))
    {
    rb.velocity = Vector3.left * 0;
    }
    if (Input.GetKeyUp(KeyCode.S))
    {
    rb.velocity = Vector3.down * 0;
    }
    if (Input.GetKeyUp(KeyCode.D))
    {
    rb.velocity = Vector3.right * 0;
    }