sebastienlachance
4/7/2015 - 7:01 PM

Unity Floatable Script

Unity Floatable Script

using UnityEngine;
using System.Collections;

public class Floating : MonoBehaviour {

	public float Amplitude = 0.3f;
	public float Speed = 2f;

	private float _initialY;
	private Transform _transform;

	void Start () {
		_transform = GetComponent<Transform> ();
		_initialY = _transform.position.y;
	}

	void Update () {
		var pos = transform.position;
		pos.y = _initialY + Amplitude * Mathf.Sin(Speed*Time.time);
		transform.position = pos;
	}
}