Verse Library verse

03 Fragment

Smoothly moves a prop toward a target using incremental steps and distance checks.

verse-library/verse-classes-and-structs-to-model-your-game-s-objects/03-fragment.verse

# Add this inside the color_chaser class.
    # Moves the prop smoothly toward a target position
    # over 100 small steps.
    # note: prop_device exposes GetTransform() and
    # MoveTo() for real positional control.
    MoveToTarget(Target : vector3)<suspends> : void =
        loop:
            CurrentTransform := MeshProp.GetTransform()
            CurrentPos := CurrentTransform.Translation
            # Stop when we are close enough (within 5 cm).
            Distance := distance(CurrentPos, Target)
            if (Distance < 5.0):
                break
            # Step 10 % of the remaining distance each tick.
            NewPos := Lerp(CurrentPos, Target, 0.1)
            NewTransform := transform{
                Translation := NewPos,
                Rotation    := CurrentTransform.Rotation,
                Scale       := CurrentTransform.Scale
            }
            MeshProp.MoveTo(NewTransform, 0.01)
            # Wait one tick before the next step.
            Sleep(0.01)

Comments

    Sign in to vote, comment, or suggest an edit. Sign in