unity first person movement code example

Example 1: unity 3d first person movement look around

public Rigidbody rb;
    public float MouseSensitivity;
    public float MoveSpeed;
    public float JumpForce;

    void Start ()
    {
        //Cursor.visible = false;
    }

    void Update()
    {
        //Look around
        rb.MoveRotation(rb.rotation * Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0)));

        //Move
        rb.MovePosition(transform.position + (transform.forward * Input.GetAxis("Vertical") * MoveSpeed) + (transform.right * Input.GetAxis("Horizontal") * MoveSpeed));
        
        //Jump
        if (Input.GetKeyDown("space"))
            rb.AddForce(transform.up * JumpForce);
    }

Example 2: fps movment unity

public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;

private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    if (controller.isGrounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= walkSpeed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}