move rigidbody2d unity code example

Example 1: rigidbody movement unity code

#region "Variables"
    public Rigidbody Rigid;
    public float MouseSensitivity;
    public float MoveSpeed;
    public float JumpForce;
    #endregion
   
    void Update ()
    {
        Rigid.MoveRotation(Rigid.rotation * Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0)));
        Rigid.MovePosition(transform.position + (transform.forward * Input.GetAxis("Vertical") * MoveSpeed) + (transform.right * Input.GetAxis("Horizontal") * MoveSpeed));
        if (Input.GetKeyDown("space"))
            Rigid.AddForce(transform.up * JumpForce);
    }

Example 2: how to drag a 2d rigidbody in unity

// add this to your player. Make sure there is a rigidbody2d attached to it. private Vector2 direction; public float force = 1000; private Rigidbody2D r;  void Start () {     r = transform.GetComponent <Rigidbody2D> (); }    void Update {     if (Input.touchCount > 0) {         target = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);         direction = (target.transform.position - transform.position).normalized;         r.AddForce (direction * Time.deltaTime * force);     } }