Skip to content

Instantly share code, notes, and snippets.

@ryupold
Last active March 21, 2016 11:54
Show Gist options
  • Save ryupold/d16f7ce4bce86162b2cf to your computer and use it in GitHub Desktop.
Save ryupold/d16f7ce4bce86162b2cf to your computer and use it in GitHub Desktop.
Unity Scripts
public class CheckHit : MonoBehaviour
protected Node CheckHit(Vector2 screenPos, int layer=-1)
{
RaycastHit hitInfo;
var hit = layer == -1 ?
Physics.Raycast(Camera.main.ScreenPointToRay(screenPos), out hitInfo, 1000f):
Physics.Raycast(Camera.main.ScreenPointToRay(screenPos), out hitInfo, 1000f, layer);
lastRay = Camera.main.ScreenPointToRay(screenPos);
if (hit)
{
return hitInfo.collider.gameObject;
}
return null;
}
}
public class Rotate : MonoBehaviour {
[SerializeField]
private Vector3 _speed = Vector3.zero;
private float _angle = 0f;
void Update () {
gameObject.transform.Rotate(_speed * Time.deltaTime);
}
}
public class TranslatePath : MonoBehaviour {
[SerializeField]
private bool _loop = true;
[SerializeField]
private bool _isPhysical = false;
private bool _useLocalScale = true;
[SerializeField]
private float _speed = 1f;
private float _allTime;
private float _currentPartTime;
private float _partTimePassed;
[SerializeField]
private Vector3[] _path = new Vector3[2];
private float[] _partTimes;
private int _currentIndex = 0;
private Vector3 _nextPos;
private int _nextIndex;
private bool _isStart, _isEnd;
[SerializeField]
private Rigidbody _ridgidBody;
void Start () {
_isStart = true;
if (_isPhysical && _ridgidBody == null)
{
try {
_ridgidBody = GetComponent<Rigidbody>();
}
catch
{
Debug.LogError("[TranslatePath]: 'Is Physical' is set to true but no ridgidBody can be found!");
}
}
RecalcPath();
}
void Update()
{
if (!_isPhysical)
{
UpdatePosition(Time.deltaTime);
}
}
void FixedUpdate()
{
if (_isPhysical)
{
UpdatePosition(Time.fixedDeltaTime);
}
}
void UpdatePosition (float deltaTime) {
if (_isEnd) return;
_allTime -= deltaTime;
if (_allTime <= 0f)
{
RecalcPath();
_isStart = true;
if(!_loop)
{
_isEnd = true;
return;
}
}
if (_isStart)
{
_currentIndex = 0;
if(_useLocalScale) gameObject.transform.localPosition = _path[_currentIndex];
_isStart = false;
}
_nextIndex = (_currentIndex + 1) % _path.Length;
_currentPartTime = _partTimes[_currentIndex];
_partTimePassed = Mathf.Clamp(_partTimePassed + deltaTime, 0, _currentPartTime);
_nextPos = Vector3.Lerp(_path[_currentIndex], _path[_nextIndex], _partTimePassed/_currentPartTime);
if (!_isPhysical) gameObject.transform.localPosition = _nextPos;
else _ridgidBody.transform.localPosition = _nextPos;
if (_partTimePassed >= _currentPartTime)
{
if (_loop)
_currentIndex = (_currentIndex + 1) % (_path.Length);
else if (_currentIndex < _path.Length - 1)
_currentIndex++;
else
_isEnd = true;
_partTimePassed = 0;
}
}
private void RecalcPath()
{
var pathLenght = 0f;
_partTimes = new float[_path.Length];
for (int i = 0; i < _partTimes.Length-1; i++)
{
_partTimes[i] = Vector3.Distance(_path[i], _path[i + 1]) / _speed;
pathLenght += _partTimes[i];
}
pathLenght += _partTimes[_partTimes.Length - 1] = _loop ? Vector3.Distance(_path[_path.Length - 1], _path[0])/_speed : 0f;
_allTime = pathLenght;
_partTimePassed = 0;
}
}
public class Utils {
public static Vector3 Vector2FromAngle(float angle)
{
return new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
}
public static float AngleFromVector2(Vector3 vector)
{
return Mathf.Atan2(vector.y, vector.x);
}
public static float AngleFromVector2Degree(Vector3 vector)
{
return Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment