revert back to old Update/Wrapping to fix broken wrapping

This commit is contained in:
Xevion
2020-05-17 00:48:46 -05:00
parent a4479aa653
commit 0a6ff38cee

View File

@@ -22,10 +22,22 @@ public class Boid : MonoBehaviour {
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
} }
void OnDrawGizmos() {
Handles.color = _isWrappingX || _isWrappingY ? Color.red : Color.white;
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(transform.position);
Handles.Label(transform.position, $"{(Vector2) viewportPosition} {(_isWrappingX ? "Y" : "N")} {(_isWrappingY ? "Y" : "N")}");
}
void Update() { void Update() {
// Updates the rotation of the object based on the Velocity // Updates the rotation of the object based on the Velocity
transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(_velocity.x, _velocity.y)); transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * -Mathf.Atan2(_velocity.x, _velocity.y));
// Skip Flock Calculations if wrapping in progress
if (_isWrappingX || _isWrappingY) {
_position += _velocity;
transform.position = _position;
}
// Acquires all Boids within the local flock // Acquires all Boids within the local flock
// List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange); // List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange);
List<Boid> flock = _parent.boids; List<Boid> flock = _parent.boids;
@@ -43,42 +55,24 @@ public class Boid : MonoBehaviour {
_velocity = (_velocity / _velocity.magnitude) * _parent.boidVelocityLimit; _velocity = (_velocity / _velocity.magnitude) * _parent.boidVelocityLimit;
} }
// Update 2D and 3D transform positions based on current velocity
_position += _velocity; _position += _velocity;
transform.position = new Vector3(_position.x, _position.y, 0); transform.position = new Vector3(_position.x, _position.y, 0);
ScreenWrap(); Wrapping();
} }
void OnDrawGizmos() { void Wrapping() {
Handles.color = _isWrappingX || _isWrappingY ? Color.red : Color.white; if (!_parent.Space.Contains(_position)) {
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(transform.position);
Handles.Label(transform.position, $"{(Vector2) viewportPosition} {(_isWrappingX ? "Y" : "N")} {(_isWrappingY ? "Y" : "N")}");
}
void ScreenWrap() {
foreach (var trenderer in _renderers)
if (trenderer.isVisible) {
_isWrappingX = false;
_isWrappingY = false;
return;
}
if (_isWrappingX && _isWrappingY)
return;
// Activate Wrap, Move // Activate Wrap, Move
Vector2 newPosition = transform.position; Vector2 newPosition = transform.position;
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(newPosition); Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(newPosition);
if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) { if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
print($"{transform.name} - Boid Wrapped on X Axis ({viewportPosition.x})");
newPosition.x = -newPosition.x; newPosition.x = -newPosition.x;
_isWrappingX = true; _isWrappingX = true;
} }
if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) { if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
print($"{transform.name} - Boid Wrapped on Y Axis ({viewportPosition.y})");
newPosition.y = -newPosition.y; newPosition.y = -newPosition.y;
_isWrappingY = true; _isWrappingY = true;
} }
@@ -86,6 +80,12 @@ public class Boid : MonoBehaviour {
transform.position = newPosition; transform.position = newPosition;
_position = newPosition; _position = newPosition;
} }
else {
// Within the rectangle again
_isWrappingX = false;
_isWrappingY = false;
}
}
Vector2 GetRandomVelocity(float magnitude) { Vector2 GetRandomVelocity(float magnitude) {
Vector2 vector = new Vector2(magnitude, magnitude); Vector2 vector = new Vector2(magnitude, magnitude);