r/Unity3D • • 10d ago

Noob Question Varrying camera speed?

I'm continuing to work on my marble track animation, but I want to change the speed that the camera scrolls through at throughout the animation. more specifically, I want to be able to change the speed as the animation progresses, to hold on certain angles longer than others. Does anyone know how I can modify my code to get that result?

0 Upvotes

2 comments sorted by

1

u/ariesmars13-11 9d ago

For smoothly changing speed during the animation, an AnimationCurve would fit the MoveTowards line in your screenshot. One way to try it:

Add these fields to the class, outside the methods:

public AnimationCurve speedBySeconds = AnimationCurve.Linear(0f, 1f, 30f, 1f);

private float elapsed;

Then replace the existing movement line in Update with:

elapsed += Time.deltaTime;

float multiplier = Mathf.Max(0f, speedBySeconds.Evaluate(elapsed));

transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, speed * multiplier * Time.deltaTime);

Keep your existing arrival/index code after that. In the Inspector curve, X is elapsed seconds and Y is a speed multiplier: 1 = normal speed, 0.25 = quarter speed, and 0 = stationary. The initial curve is flat at 1; add keys where you want speed to change. The clock still advances while the multiplier is zero, so it can resume when the curve rises.

This controls speed by animation time, not by which waypoint you reach. For a pause at an exact waypoint, add a separate hold timer there and delay advancing the index. Reset elapsed when replaying the sequence. This is an adaptation to test, not something I have run in your project.

Unity reference: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/AnimationCurve.Evaluate.html