mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-07 20:06:26 -06:00
revert back to old Update/Wrapping to fix broken wrapping
This commit is contained in:
@@ -22,14 +22,26 @@ public class Boid : MonoBehaviour {
|
||||
transform.name = $"Boid {transform.GetSiblingIndex()}"; // Name the Game Object so Boids can be tracked somewhat
|
||||
}
|
||||
|
||||
void Update() {
|
||||
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() {
|
||||
// Updates the rotation of the object based on the Velocity
|
||||
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
|
||||
// List<Boid> flock = GetFlock(parent.boids, parent.boidGroupRange);
|
||||
List<Boid> flock = _parent.boids;
|
||||
|
||||
|
||||
if (flock.Count > 0) {
|
||||
// Calculate all offsets and multiple by magnitudes given
|
||||
Vector2 r1 = Rule1(flock) * _parent.cohesionBias;
|
||||
@@ -43,48 +55,36 @@ public class Boid : MonoBehaviour {
|
||||
_velocity = (_velocity / _velocity.magnitude) * _parent.boidVelocityLimit;
|
||||
}
|
||||
|
||||
// Update 2D and 3D transform positions based on current velocity
|
||||
_position += _velocity;
|
||||
transform.position = new Vector3(_position.x, _position.y, 0);
|
||||
|
||||
ScreenWrap();
|
||||
Wrapping();
|
||||
}
|
||||
|
||||
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 Wrapping() {
|
||||
if (!_parent.Space.Contains(_position)) {
|
||||
// Activate Wrap, Move
|
||||
Vector2 newPosition = transform.position;
|
||||
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(newPosition);
|
||||
|
||||
void ScreenWrap() {
|
||||
foreach (var trenderer in _renderers)
|
||||
if (trenderer.isVisible) {
|
||||
_isWrappingX = false;
|
||||
_isWrappingY = false;
|
||||
return;
|
||||
if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
|
||||
newPosition.x = -newPosition.x;
|
||||
_isWrappingX = true;
|
||||
}
|
||||
|
||||
if (_isWrappingX && _isWrappingY)
|
||||
return;
|
||||
if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
|
||||
newPosition.y = -newPosition.y;
|
||||
_isWrappingY = true;
|
||||
}
|
||||
|
||||
// Activate Wrap, Move
|
||||
Vector2 newPosition = transform.position;
|
||||
Vector3 viewportPosition = _parent.cam.WorldToViewportPoint(newPosition);
|
||||
|
||||
if (!_isWrappingX && (viewportPosition.x > 1 || viewportPosition.x < 0)) {
|
||||
print($"{transform.name} - Boid Wrapped on X Axis ({viewportPosition.x})");
|
||||
newPosition.x = -newPosition.x;
|
||||
_isWrappingX = true;
|
||||
transform.position = newPosition;
|
||||
_position = newPosition;
|
||||
}
|
||||
|
||||
if (!_isWrappingY && (viewportPosition.y > 1 || viewportPosition.y < 0)) {
|
||||
print($"{transform.name} - Boid Wrapped on Y Axis ({viewportPosition.y})");
|
||||
newPosition.y = -newPosition.y;
|
||||
_isWrappingY = true;
|
||||
else {
|
||||
// Within the rectangle again
|
||||
_isWrappingX = false;
|
||||
_isWrappingY = false;
|
||||
}
|
||||
|
||||
transform.position = newPosition;
|
||||
_position = newPosition;
|
||||
}
|
||||
|
||||
Vector2 GetRandomVelocity(float magnitude) {
|
||||
|
||||
Reference in New Issue
Block a user