- Not to be confused with Gravity.
Velocity is defined as speed in a given direction, and can, therefore, be negative, unlike speed alone (which is directionless and always positive). It is often used in projects for physics effects. Using velocity is a far smoother and aesthetically pleasing method than traditional scrolling.
How to Program Velocity in Scratch
A common method used to program velocity is by maintaining its value as a variable and using that to change the sprite's position:
when green flag clicked forever if <key (left arrow v) pressed?> then change [velocity v] by (-1) //Reduce velocity for accelerating towards the left end if <key (right arrow v) pressed?> then change [velocity v] by (1) //Increase velocity for accelerating towards the right end set [velocity v] to ((velocity) * (0.9)) //Gradually lose speed (regardless of direction) change x by (velocity) //Change the sprite's position based on the updated velocity
The above technique is simple, efficient, and contained within a single script. The numbers can be changed to affect how quickly the sprite can change its velocity. It should be noted that the number in the last set variable block must be between 0 and 1, or the sprite will speed up rather than slowing down gradually.
Velocity has many uses, and it can be used almost anywhere. A few of the many uses are below:
- Racing and driving games — Vehicles with velocity are more realistic to drive with.
- Platformers — Velocity can make movement (especially vertical movement) more realistic.
- Physics simulations — A virtual ball can bounce and follow gravity with velocity.
- Scrolling projects — Scrollers with velocity are popular.
- Fidget Spinners — Fidget Spinners run much more smoothly with angular velocity.
- Space games — Space games such as "Asteroids" look more realistic using velocity.
In general, anywhere that a sprite is moving can be enhanced with velocity.
Below are some projects that use velocity:
- Velocity engine
- Velocity Tutorial
- Super Smash Bros. Ball!
- Off-Road Rally
- Ice-olated V 1.0
- Velocity Test
- Velocity Script