First Script
So here is the challenge, we want to create a box, and make a simple repetitive motion going up and down continuously. This is called a sine function. So by giving the position of the box a sine function, then the box will oscillate up and down.
There are a few ways to skin a cat so let’s look at a couple of ways and understand what we’ve done
void Update()
{
float amplitude = 5f; // Adjust the amplitude as needed
float speed = 2f; // Adjust the speed as needed
// Calculate the new y position
float newY = Mathf.Sin(Time.time * speed) * amplitude;
// Set the object's position
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
}
Or the following:
public class SineMovement : MonoBehaviour
{
public float amplitude = 1f; // The height of the sine wave
public float frequency = 1f; // The speed of the sine wave
public Vector3 movementVector = Vector3.up //The direction of the movement
private Vector3 startPositiopn
void Start()
{
startPosition = transform.position;
}
void Update()
{
// Calculate the new position using a sine wave
float sine = Mathf.Sin(Time.time * frequency);
Vector3 offset = movementVector * sine * amplitude;
transform.position = startPosition + offset;
}
}

