Move an object in a sine wave oscillating around arbitrary vector.


I want my gameobject to move in a sine wave pattern, this i can do. But i want to oscillate the wave around an arbitrary axis. See my picture below of how i want it to move, where the black lines are the arbitrary axes, and red curve is the sine.

Any math ninjas who can help me?


If that curve is flat, y == 0 for instance, you can have the normal of a point on the path with a cross vector between the vector from that point to the next and the vector up. From there, the sin curve will give you how much you have to add that normal the the point’s position.

Hope I’m clear enough :stuck_out_tongue:

When I solve problems like this one, I tend to separate out the different kinds of motions. So I would create an empty game object and attach a script that walks the path with the forward vector always pointing down the path. Then I would make the visible object a child of this the empty game object and assign it a script that just had back and forth sine motion. Here is a script that will move back and forth in a sine motion along an arbitrary axis. If you are using X,Y,or Z for your sine motion, you can simplify it a bit.

public class SineMovement : MonoBehaviour {
	float fMagnitude = 0.75f;
	Vector3 v3Axis = new Vector3(1.0f, 1.0f, 1.0f);
	void Start() {

	void Update () {
		transform.localPosition = v3Axis * Mathf.Sin (Time.time) * fMagnitude;