private void UpdateVelocity(PlatformInstruction currentInstruction) { float xv = 0f; float yv = 0f; float xp = rigidBody.position.x; float yp = rigidBody.position.y; // Move x towards instruction's x position if (!transform.position.x.IsWithin(positionTolerance, currentInstruction.Position.x)) { if (transform.position.x < currentInstruction.Position.x) { xv = currentInstruction.MovementSpeed; } else if (transform.position.x > currentInstruction.Position.x) { xv = -currentInstruction.MovementSpeed; } } else { xp = currentInstruction.Position.x; } // Move y towards y position if (!transform.position.y.IsWithin(positionTolerance, currentInstruction.Position.y)) { if (transform.position.y < currentInstruction.Position.y) { yv = currentInstruction.MovementSpeed; } else if (transform.position.y > currentInstruction.Position.y) { yv = -currentInstruction.MovementSpeed; } } else { yp = currentInstruction.Position.y; } rigidBody.position = new Vector2(xp, yp); rigidBody.velocity = new Vector2(xv, yv); }
private bool IsNearDestination(PlatformInstruction currentInstruction) => (transform.position.x.IsWithin(positionTolerance, currentInstruction.Position.x)) && (transform.position.y.IsWithin(positionTolerance, currentInstruction.Position.y));