Table of Contents

Your first Luny Script in Unity

Goal: You want to create a controllable capsule as your first "Player" in a blank scene, using a regular MonoBehaviour script.

Status Quo: Unity 6.3 LTS

What you have to do in Unity to set up player and scene:

  • Project: Right-click: Create -> Scene -> Scene. Name it whatever you like.
  • Project: Right-click: Create -> MonoBehaviour Script. Name it "Player".
  • Hierarchy: Right-click: 3D Object -> Capsule. Name it "Player".
  • Inspector: Select "Player". Inspector: Add Component -> Player.

Then you check the Internet for a "simple" Unity 'Player' script and found this:

using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour 
{
    private InputAction moveAction;
    private InputAction jumpAction;
    private InputAction crouchAction;

    void Awake() 
    {
        moveAction = InputSystem.actions.FindAction("Move");
        jumpAction = InputSystem.actions.FindAction("Jump");
        crouchAction = InputSystem.actions.FindAction("Crouch");
    }

    void Update() 
    {
        Vector2 rawMoveInput = moveAction.ReadValue<Vector2>();
        float jumpInputValue = jumpAction.ReadValue<float>();
        float crouchInputValue = crouchAction.ReadValue<float>();

        float moveX = rawMoveInput.x;
        float moveZ = rawMoveInput.y;

        Vector3 movementDirection = new Vector3(0, 0, 0);
        movementDirection.x = moveX;
        movementDirection.z = moveZ;

        if (jumpInputValue > 0.1f) 
        {
            movementDirection.y = 1.0f;
        }
        else 
        {
            if (crouchInputValue > 0.1f) 
            {
                movementDirection.y = -1.0f;
            }
            else 
            {
                movementDirection.y = 0.0f;
            }
        }

        float movementSpeed = 4.0f;
        float timeStep = Time.deltaTime;
        Vector3 finalMovement = movementDirection * timeStep * movementSpeed;

        transform.Translate(finalMovement);
    }
}

It's already 52 lines for something so trivial. 🤯

With LunyScript

Let's compare this with the same LunyScript:

using LunyScript;

public partial class Player : Script
{
    public override void Build(ScriptBuildContext context)
    {
        On.FrameUpdate(
            Transform.MoveBy(Input.Direction("Move"), 4),
            Transform.MoveUp(Input.Button("Jump").Strength, 4),
            Transform.MoveDown(Input.Button("Crouch").Strength, 4)
        );
    }
}

That's 13 lines, four times less! 😎