Unity 2D: Run while holding key and having energy left


Unity 2D: Run while holding key and having energy left

I decided to try and add a Run feature to my 2D controller today.
Couldn’t find what I wanted to achieve online, but after trial and error I got something working.

This is using the “new” input system. Some code is not shown, but you get the idea.

– Hold key down while moving to enable running.
– While running, runTimeCounter clocks up.
– Once runTimeCounter hits runTimeLimit, you can no longer run.
– When isRunning is false, runTimeCounter regenerates (Goes back to 1f, thus enabling running again)

今日は2Dコントローラの走るを実装してみた。
Unityの新Input Systemで求めていたコードがなかったので、試行錯誤でやってみたけどもっと良い書き方があるのは明確だ。上達したら見直す。

・キーを押しっぱなしの時に走る
・走っている最中 runTimeCounterの数値が上がる
・runTimeLimitに到達すると走れなくなる
・走っていない状態でrunTimeCounterの数値が下がる

    float runTimeLimit = 4f;
    float runTimeCounter = 1f;
    float horizontal;
    bool isRunning = false;

    void FixedUpdate()
    {
        // Walk, Run action
        if (isRunning && runTimeCounter <= runTimeLimit)
        {
            runTimeCounter += Time.deltaTime;
            rb.velocity = new Vector2(runSpeed * horizontal * speed,   rb.velocity.y);

            if (runTimeCounter >= runTimeLimit)
            {
                isRunning = false;
            }
        }
        else
        {
            rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);

            while (runTimeCounter > 0f)
            {
                runTimeCounter -= Time.deltaTime;
            }
        }

        IsGrounded();
        WallSlide();
    }

    public void Run(InputAction.CallbackContext context)
    {

        if (context.performed)
        {
            isRunning = true;
        }

        if (context.canceled)
        {
            isRunning = false;
        }
    }

Priorities and what to work on?


I’ve been working on my 2D movement controller, but having a rough time.
My todo list is still small, but I could see it increasing everytime I realize something new that a 2D platformer should have.
Now the question is, should a beginner game dev be studying art design, sound design at the same time?  Or just work with blocks to prototype.

I read that prototyping is key because you wouldn’t want to invest so much time into art, when the game is simply boring.  But at the same time, you have to feel some love.
I am going to approach my studies by rotation between art work tutorials, and the coding.  This way, if you get stuck, you could study something else and challenge your problem again the following day with a fresh mindset.

Separating palette/tilemap with friction.


For study purposes, I will be making a 1 or 2 stage 2D platformer.
Yes, very typical and sort of like a Hello World project for game dev right.
There’s a lot to leran in each step.
I’ll be starting with the Movement Controller, but I got sidetracked on why my character gets stuck when hitting a wall.  It looks like I need to add a Physics 2D material to the tilemap.

My ground and wall tiles were all in the same palette, so I created a second palette with the Physics 2D Material added with Friction set to 0.
Don’t know if this is the best way for my problem, but it works for now.