Plant Interaction
To bring the world to life more we want the plants to sway as the player runs past them.
The code below achieves this and is explained afterwards.
Code Purpose:
This code makes the vegetation swing when the player collides with it, based on the player's movement speed and direction. It ensures that the vegetation only swings if the player is moving and it's not already swinging. The swinging motion is smooth and natural, with a back-and-forth effect, giving a more dynamic feel to the environment.
Action Explained:
Tween one property: Vegetation's "offsetAngle"
If the above conditions are true (the player is moving, colliding with vegetation, and the vegetation isn’t already swinging), the code applies a tween (smooth transition) to the vegetation's offsetAngle (the angle of the object).
End value: The angle changes based on the player's movement direction and speed:
Self.Angle + sign(PlayerCollision.Platform.VectorX) * 10
Self.Angle: The current angle of the vegetation.
sign(PlayerCollision.Platform.VectorX): This checks the direction of the player's horizontal movement. It returns 1 if moving right and -1 if moving left.
* 10: Multiplies the movement direction by 10, meaning the vegetation will swing more or less depending on the player's speed and direction.
Time: The swing happens over a random time between 0.15 and 0.25 seconds, making the swing feel more natural and varied.
Ease: The easing is set to "easeinoutsine", meaning the swing starts slowly, speeds up, and then slows down again, simulating a natural swinging motion.
Ping-pong: The "ping-pong" option is set to yes, so the tween will swing the vegetation back and forth (like real vegetation would after being hit).
Repeat count: It's set to 1, so the swing will happen once in both directions.