Unity: Cinemachine Confiner

Today, I wante to learn how to control cinemachine so that it doesn’t exceed a certain x or y transform value. My first idea was to manually place borders and have a detection script.
But then, I searched youtube and found CodeMonkey’s tutorial on how to use the Cinemachine Confiner. It’s beautiful! Easy to setup. Thank you.L

CodeMonkey’s tutorial on Cinemachine’s confiner.
How to use Cinemachine Confiner (Don’t let Players see the VOID!)

2D Platformer: Double Jump (New input system)

Today, I tested a double jump feature for a 2D platformer controller.
I haven’t had extensive amount of experience with the legacy input system, but I have to say the new input system is confusing at first. Or am I still confused.
At the end, the new input system seems to be the path to take if you want an easy implementation of a variety of hardware controllers users may choose to use.

    public void Jump(InputAction.CallbackContext context)
    {
        // Jump only if grounded
        if (context.performed && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpPower);
            canDoubleJump = true;
        }

        // Shorter jump height if jump is tapped
        if (context.canceled && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }

        // Double jump
        if (!IsGrounded() && context.performed && canDoubleJump)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpPower);
            canDoubleJump = false;
        }
    }

Spring Joint and Invoking

Due to time investment into app dev, it has been forever since opening Unity.
As reckless as it may sound, I decided to join a game jam coming up soon.
With that said, time to study the super basics.

Today, I learned how to use Spring Joint, and Invoke to call methods with a specified delay.
Something I did today and tested on my iphone.

    private void Example()
    {
        Invoke(nameof(RunMethod), 1.8f);
    }

    void RunMethod()
    {
	Console.WriteLine("It's working");
    }

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;
        }
    }