Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created July 16, 2013 07:50
Show Gist options
  • Save kirillrybin/6006691 to your computer and use it in GitHub Desktop.
Save kirillrybin/6006691 to your computer and use it in GitHub Desktop.
Unity3D / Левитация / Levitate
using UnityEngine;
using System.Collections;
public class Levitate : MonoBehaviour
{
private const float SpeedMultiplier = .05F;
private float _speedX = 0.0F;
private float _speedY = 0.0F;
private Vector2 _upperLeftLimit;
private Vector2 _bottomRightLimit;
void Start()
{
var pos = transform.localPosition;
_upperLeftLimit = new Vector2(pos.x - 5, pos.y - 5);
_bottomRightLimit = new Vector2(pos.x + 5, pos.y + 5);
}
void Update()
{
_speedX += Random.Range(0, SpeedMultiplier) - SpeedMultiplier / 2;
_speedY += Random.Range(0, SpeedMultiplier) - SpeedMultiplier / 2;
var pos = transform.localPosition;
if (pos.x + _speedX > _upperLeftLimit.x && pos.x + _speedX < _bottomRightLimit.x)
pos.x += _speedX;
else
_speedX = 0;
if (pos.y + _speedY > _upperLeftLimit.y && pos.y + _speedY < _bottomRightLimit.y)
pos.y += _speedY;
else
_speedY = 0;
transform.localPosition = pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment