Skip to content

Instantly share code, notes, and snippets.

@arunkarnann
Last active March 8, 2017 12:52
Show Gist options
  • Save arunkarnann/456dc8a7b865c7ac5b95fc725e92da71 to your computer and use it in GitHub Desktop.
Save arunkarnann/456dc8a7b865c7ac5b95fc725e92da71 to your computer and use it in GitHub Desktop.
Birds flying in sine wave form Unity3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyBirds : MonoBehaviour {
Vector3 firstPos;
[SerializeField]
Vector3 lastPos;
[SerializeField]
float xSpeed=5f;
/// <summary>
/// The distance at which the object should move up and down
/// </summary>
[SerializeField]
float yLimit=50f;
[SerializeField]
float cosineMultiplier=2f;
float elapsedTime;
///Reduce to increase the wave length in y axis
float cosineValue=0.5f;
void Update () {
elapsedTime += Time.deltaTime;
cosineValue = Mathf.Cos( Mathf.PI * elapsedTime * cosineMultiplier ) ;
Vector3 newPosition = new Vector3(lastPos.x ,(lastPos.y + yLimit) * cosineValue,lastPos.z);
transform.position = Vector3.MoveTowards (transform.position, newPosition, xSpeed * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment