Moving things
On objects and behaviours alike:
class Drone extends Behaviour { final _follow = SmoothDamp3(smoothTime: 0.3);
@override void onUpdate() { super.onUpdate(); translate(Vec3.forward * time.delta); // the way it looks rotate(Vec3.up, 0.5 * time.delta); // about its own up final player = world.first<Pilot>(); if (player == null) return; lookAt(player.position); position = _follow.step(position, player.position + Vec3(0, 3, 0), time.delta); }}
class Pilot extends GameObject {}- As Unity’s:
positionandrotationare in the world, whatever the object’s parents;localPosition,localRotationandlocalScaleare relative to its parent, andlossyScaleis its size in the world.forward,rightandupare in the world. For a root the two are the same. translateandrotatego by the object’s own axes; giverelativeTo: Space.worldfor the world’s.lookAt(point),transformPointandinverseTransformPointtoo.parent = otherkeeps the object where it is in the world, as Unity’s;setParent(other, worldPositionStays: false)keeps its local transform instead, so it moves with its new parent.- The local transform is one value, a position, a rotation and a scale:
transform = Transform(position: …, rotation: …)sets it at once, andworldTransformreads the world’s. - A rotation is a
Quat, as in Unity:Quat.lookRotation(direction)to face a way,Quat.euler(pitch, yaw, roll)by angles (radians), andQuat.axisAngle(axis, angle). A camera ateyelooking attargetisTransform.lookAt(eye, target).rotation.eulerAnglesreads the angles back (x the pitch, y the yaw, z the roll, in radians), sorotation.eulerAngles.yis the way an object faces;Quat.fromTo(Vec3.up, normal)turns one direction to another, andQuat.rotateTowards(rotation, goal, turnSpeed * time.delta)turns by steps, never past. v.withY(0.5)(andwithX,withZ) changes one part,v.flatputs it on the ground (y = 0) for walking, andVec3.all(2)is a uniform scale.- A zero vector has no direction:
normalizedthrows for one, so a vector that should never be zero says so where it is. Where one can be, as the way to a target the object may have reached, writenormalizedOrZero, which gives zero (as Unity’snormalized):position += (target - position).normalizedOrZero * speed * time.delta.lookAtthe point the object is at leaves it as it is. AVec2hasnormalizedandnormalizedOrZerotoo. color.withAlpha(0.5)is the colour half see-through (Unity’scolor.a = 0.5).- Unity’s Mathf:
lerp,inverseLerp,clamp01,moveTowards,repeat,pingPong,randomInSphere,randomOnCircle, andVec3.lerp,Vec3.moveTowards,Quat.slerp,Color.lerp.SmoothDampandSmoothDamp3keep their velocity between frames.