Moving along a sine curve?

Hello. I have a few questions about object movement.

1: How can I make an object move along a sine curve? I want to be able to define the objects’s speed (how fast it will move along the x axis), amplitude (the difference in the heights that it rises/dips to), and the number of times it will make the rise/fall cycle. I then have it move along that path (the z value will be constant). A good example of this type of movement can be seen here around the 6:45 mark (I want my objects to move just like the blue squares do in this game).

2: How can I define a point in space as the center, define a value as the radius, and then define the object’s speed and have an object loop around that point (without rotating itself)?

My end goal is a bit complex, but I think it will work)…my question being, what do you guys think of it? Anyways, first I will use this to define the corner positions of the camera’s viewport so that I can calculate any position in the (the very center of the screen, three quarters from the right/one half up, etc). Then, I will use the ideas posted above to move the object’s in interesting paths along the screen. For example, I could have an object spawn at the very center of the screen’s vertical space (on the far left, just off the screen) and have it move right while bobbing up and down to the 25 percent/75 percent points on the screen a total of three times. Another example would be moving out to the very center of the screen’s x distance (along the 25 percent vertical line…so no sine wave this time), loop around the center of the screen three times and then continue moving right.

Thanks.

It’s probably easiest to describe what to do with some code. So here you go:

public class SinWaveMover : MonoBehaviour
{
	void Start()
	{
		m_centerPosition = transform.position;
	}
	
	void Update()
	{
		float deltaTime = Time.deltaTime;
		
		// Move center along x axis
		m_centerPosition.x += deltaTime * m_speed;
		
		// Update degrees
		float degreesPerSecond = 360.0f / m_period;
		m_degrees = Mathf.Repeat(m_degrees + (deltaTime * degreesPerSecond), 360.0f);
		float radians = m_degrees * Mathf.Deg2Rad;
		
		// Offset by sin wave
		Vector3 offset = new Vector3(0.0f, m_amplitude * Mathf.Sin(radians), 0.0f);
		transform.position = m_centerPosition + offset;
	}
	
	Vector3 m_centerPosition;
	float m_degrees;
	
	[SerializeField]
	float m_speed = 1.0f;
	
	[SerializeField]
	float m_amplitude = 1.0f;
	
	[SerializeField]
	float m_period = 1.0f;
}
public class CircleMover : MonoBehaviour
{
	void Start()
	{
		m_centerPosition = transform.position;
	}
	
	void Update()
	{
		// Update degrees
		float degreesPerSecond = 360.0f / m_period;
		m_degrees = Mathf.Repeat(m_degrees + (Time.deltaTime * degreesPerSecond), 360.0f);
		float radians = m_degrees * Mathf.Deg2Rad;
		
		// Offset on circle
		Vector3 offset = new Vector3(m_radius * Mathf.Cos(radians), m_radius * Mathf.Sin(radians), 0.0f);
		transform.position = m_centerPosition + offset;
	}
	
	Vector3 m_centerPosition;
	float m_degrees;
	
	[SerializeField]
	float m_radius = 1.0f;
	
	[SerializeField]
	float m_period = 1.0f;
}

Thanks for the reply. I tried converting your first script into JS (that is what I am more familiar with), but I ran into some problems. What did I do wrong?

function Update(){
		var m_centerPosition = transform.position;
		
	   	deltaTime = Time.deltaTime;

        // Move center along x axis
        m_centerPosition.x += deltaTime * m_speed;

        // Update degrees
        float degreesPerSecond = 360.0f / m_period;

        m_degrees = Mathf.Repeat(m_degrees + (deltaTime * degreesPerSecond), 360.0f);

       radians = m_degrees * Mathf.Deg2Rad;

        // Offset by sin wave
        Vector3 offset = new Vector3(0.0f, m_amplitude * Mathf.Sin(radians), 0.0f);

        transform.position = m_centerPosition + offset;
        
        Vector3 m_centerPosition;

    	float m_degrees;

 
    	[SerializeField]

    	float m_speed = 1.0f;

    	[SerializeField]

    	float m_amplitude = 1.0f;

    	[SerializeField]

    	float m_period = 1.0f;
}

Anyone?