githubEdit

C# to TypeScript

Translate your C# knowledge to TypeScript for Needle Engine

Translate your Unity C# knowledge to TypeScript for web development with Needle Engine.

:::tip New to TypeScript? Start with TypeScript Essentialsarrow-up-right for core language concepts without C# comparisons. :::


Value Types vs Reference Types

C# - Value Types (Structs)

In C#, Vector3 is a struct (value type)—it gets copied when passed to methods:

void MyCallerMethod() {
    var position = new Vector3(0, 0, 0);
    MyExampleVectorMethod(position);
    Debug.Log("Position.x is " + position.x); // ✅ Still 0 (copy was modified)
}

void MyExampleVectorMethod(Vector3 position) {
    position.x = 42; // Modifies the COPY, not the original
}

Key behavior: Assignment creates a copy:

var myVector = new Vector3(1, 1, 1);
var myOtherVector = myVector;  // Creates a COPY
myOtherVector.x = 42;
// Logs: 1, 42 (two separate instances)
Debug.Log(myVector.x + ", " + myOtherVector.x);

TypeScript - Reference Types (Objects)

In JavaScript/TypeScript, all objects are reference types—including Vector3:

Key behavior: Assignment creates a reference:

:::warning Critical Difference! In TypeScript, modifying a Vector modifies the original. To create a copy, use .clone():

:::


Vector Math & Operators

C# - Operator Overloading

C# supports operator overloading for vectors:

TypeScript - Method Calls

JavaScript/TypeScript does not support operator overloading. Use methods instead:

Common Vector Operations

Operation
Unity (C#)
Needle Engine (TypeScript)

Multiply

vector *= 2

vector.multiplyScalar(2)

Add

vector += other

vector.add(other)

Subtract

vector -= other

vector.sub(other)

Length

vector.magnitude

vector.length()

Normalize

vector.normalized

vector.normalize()

Distance

Vector3.Distance(a, b)

a.distanceTo(b)

:::tip three.js Vector Methods Most three.js vector methods modify the original vector. For immutable operations, clone first:

:::


Time & Delta Time

Unity Time

Needle Engine Time

Access time data via this.context.time:

Property Mapping

Unity (C#)
Needle Engine (TypeScript)
Description

Time.time

this.context.time.time

Scaled time since app started

Time.deltaTime

this.context.time.deltaTime

Time since last frame

Time.frameCount

this.context.time.frameCount

Total frames rendered

Time.realtimeSinceStartup

this.context.time.realtimeSinceStartup

Unscaled time

Time.timeScale

this.context.time.timeScale

Time multiplier

:::tip Frame-Rate Independent Movement Always multiply movement by deltaTime:

:::

Read more about Timearrow-up-right


Raycasting

Unity Physics.Raycast

Needle Engine Raycast

Key differences:

  • Unity: Requires colliders for all raycasts

  • Needle Engine: Default raycasts hit visible geometry, optional physics-based raycasts require colliders

Read more about Raycastingarrow-up-right


Input

Unity Input

Needle Engine Input

Polling input state:

Event subscription:

Read more about Inputarrow-up-right


InputSystem Callbacks

Unity IPointerClickHandler

Needle Engine Pointer Events

Available events:

  • onPointerDown

  • onPointerUp

  • onPointerEnter

  • onPointerMove

  • onPointerExit

  • onPointerClick

All receive a PointerEventData argument.

Read more about Input Eventsarrow-up-right


Debug.Log

Unity Debug

JavaScript Console


Debug Gizmos

Unity Gizmos

Needle Engine Gizmos

Key differences:

Unity
Needle Engine

OnDrawGizmos() method

Call Gizmos from anywhere

OnDrawGizmosSelected()

Use if(isDevEnvironment()) to filter

Only in Editor

Visible in browser (filter for production!)

Available methods:

  • DrawArrow - Arrow between two points

  • DrawBox / DrawBox3 - Wireframe boxes

  • DrawDirection - Direction arrow from origin

  • DrawLine - Line between two points

  • DrawRay - Infinite ray from origin

  • DrawSphere / DrawWireSphere - Solid/wireframe spheres

See full Gizmos APIarrow-up-right


Useful Utility Methods

Common Unity Methods

Unity (C#)
Needle Engine (TypeScript)

Check if in Editor

#if UNITY_EDITOR

Get URL parameter

N/A

Check mobile device

Application.isMobilePlatform

Check iOS

Application.platform == RuntimePlatform.IPhonePlayer

Example:

Read more about Platform Detectionarrow-up-right


Quick Reference Cheat Sheet

Task
Unity (C#)
Needle Engine (TypeScript)

Component

MonoBehaviour

Behaviour

GameObject

GameObject

Object3D

Transform

transform.position

this.gameObject.position

World Position

transform.position

this.gameObject.worldPosition

Find Component

GetComponent<T>()

getComponent(T)

Time

Time.deltaTime

this.context.time.deltaTime

Input

Input.GetMouseButtonDown(0)

this.context.input.getPointerDown(0)

Raycast

Physics.Raycast(...)

this.context.physics.raycast(...)

Debug Log

Debug.Log(...)

console.log(...)

Gizmos

OnDrawGizmos()

Gizmos.Draw...()

Vector Add

vector += other

vector.add(other)

Vector Multiply

vector *= 2

vector.multiplyScalar(2)


What's Next?

Continue learning:

Reference:

Last updated