-
-
Save kirillrybin/6006691 to your computer and use it in GitHub Desktop.
Unity3D / Левитация / Levitate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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