Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.37k stars 352 forks source link

Moving on a Sinewave #410

Open jlubeck opened 4 years ago

jlubeck commented 4 years ago

I'm trying to make my objects move on Sinewave Path between 2 given Vector3, but I can't seem to get it right. All the tutorials and posts I find online mention doing it on the Update method, but that doesn't help me since I need to create an array of Waypoints to feed the DOPath method here.

this is what I have so far:

                float a = Mathf.Atan2(positionEnd.y - positionStart.y, positionEnd.x - positionStart.x);
                float frequency = 3;
                float amplitude = 5;

                List<Vector3> vertices = new List<Vector3>();
                Vector3 previousVertex = positionStart;
                for (float i = 0; i <= distance; i += 1f)
                {
                    Vector3 vertex = new Vector3(i, Mathf.Sin(i * (Mathf.PI * 2) * frequency / distance) * amplitude);
                    vertices.Add(vertex);
                    previousVertex = vertex;
                }
                vertices.Reverse();

                Quaternion rot = Quaternion.AngleAxis(a * Mathf.Rad2Deg, Vector3.forward);
                Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
                int v = 0;
                Vector3[] newVerts;
                newVerts = new Vector3[vertices.Count];
                while (v < vertices.Count)
                {
                    Vector3 pt = m.MultiplyPoint3x4(vertices[v]);
                    newVerts[v] = m.MultiplyPoint3x4(pt);
                    v++;
                }

                currentTween = gameObject.transform.DOPath(newVerts, time, DG.Tweening.PathType.Linear, PathMode.Full3D, 5, new Color(1, 1, 1)).SetLookAt(0.01f);

But that gives me this: Screen Shot 2020-06-15 at 4 23 24 PM

For some reason the wave starts on a different angle than were it should. This code was working with a different tweening library which created a Spline with those vertices. But now that I switched to DOTween, it's behaving like that.

Can anyone help me please? More than willing to change the whole sinewave code for something else as long as it makes it work

Thanks!

AnEmortalKid commented 2 months ago

Your mileage may vary, I kept X static and just move Y in a sine wave pattern

public class FishSwimAnimationController : MonoBehaviour
{
    [SerializeField]
    [Tooltip("Spot to move towards")]
    private Transform destination;

    [SerializeField]
    [Tooltip("Duration of the movement towards the target")]
    public float moveDuration = 5f;

    [SerializeField]
    [Tooltip("Amplitude of the sine wave")]
    public float waveAmplitude = 2f;

    [SerializeField]
    [Tooltip("Frequency of the sine wave")]
    public float waveFrequency = 2f;

    private Vector3 startPosition;

    private void Awake()
    {
        startPosition = transform.position;
    }

    void Start()
    {
        // Get the target position
        Vector3 targetPosition = destination.position;

        // move towards X linearly
        transform.DOMoveX(targetPosition.x, moveDuration).SetEase(Ease.Linear);

        // Move along y axis following the sine wave
        DOVirtual.Float(0, moveDuration, moveDuration, (t) =>
        {
            // Calculate the new y position using the sine function and apply the shift to our og y
            float newY = waveAmplitude * Mathf.Sin(t * waveFrequency * Mathf.PI * 2);
            transform.position = new Vector3(transform.position.x, startPosition.y + newY, startPosition.z);
        }).SetEase(Ease.Linear);
    }
}

you can also edit the float callback to also compute newX , which I think in your case maybe you might want to?