Open jlubeck opened 4 years 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?
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:
But that gives me this:
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!