my code

Snippet options

Download: Download snippet as charactercontroller-2d.c.
Copy snippet: For this you need a free my code account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

    //how fast player can move(you can change these in the inspector)
    float maxSpeed;

    //jump force, again: you can change this in the inspector due to serialization
    float jumpForce;

    //check if player is grounded
    bool isGrounded = false;

    float groundRadius = 0.2f;
    public Transform groundCheck;
    public LayerMask whatIsGround;

    Animator anim;

    private void Start()
        anim = GetComponent<Animator>();

    //physics in fixedupdate
    private void FixedUpdate()
        float move = Input.GetAxis("Horizontal");

        GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        /*anim.SetBool("Ground", isGrounded);
        anim.SetFloat("Speed", Mathf.Abs(move));
        anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);*/

    private void Update()
        if(isGrounded && Input.GetKeyDown(KeyCode.Space))
            anim.SetBool("Ground", false);
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));


Create a free my code account now.

my code is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.