I modified the math for the player laser so the laser and collider grow relative to their start sizes.
I then made the laser shrink before fading out.
My math for the shrinking code is a little off, it moves slightly and the collider doesn’t shrink the exact way I wanted (it jumps to the end instead of shrinking with the tail).
Here you can tell it jumps slightly.
Of course you can’t really tell when it moves slightly faster.
Here’s the code if anyone is interested or has any suggestions to fix the 2 issues.
private void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
_capsuleCollider = GetComponent<CapsuleCollider2D>();
_size = _startSize = _spriteRenderer.size;
_capsuleSize = _capsuleStartSize = _capsuleCollider.size;
_capsuleOffset = _capsuleStartOffset = _capsuleCollider.offset;
_capsuleSizeMultiplier = _capsuleSize.x / _size.x;
}
private void OnEnable()
{
//_laserRange = _startSize.x > 1 ? _startSize.x + 8 : 8;// ActiveWeapon.Instance.WeaponInfo.weaponRange;
StopAllCoroutines();
_laserRange = _startSize.x > 1
? _startSize.x + ActiveWeapon.Instance.WeaponInfo.weaponRange
: ActiveWeapon.Instance.WeaponInfo.weaponRange;
FaceMouse();
StartCoroutine(IncreaseLaserLengthRoutine());
}
private IEnumerator IncreaseLaserLengthRoutine()
{
yield return null;
float timePassed = 0;
while (_size.x < _laserRange)
{
timePassed += Time.deltaTime;
var linerT = timePassed / laserGrowTime;
_size.x = Mathf.Lerp(_startSize.x, _laserRange, linerT);
_capsuleSize.x = _size.x * _capsuleSizeMultiplier;
_capsuleOffset.x = (_size.x * _capsuleStartOffset.x) / _startSize.x;
_spriteRenderer.size = _size;
_capsuleCollider.size = _capsuleSize;
_capsuleCollider.offset = _capsuleOffset;
yield return null;
}
var myTransform = transform;
myTransform.position = new Vector3(myTransform.position.x + myTransform.right.x * _laserRange,
myTransform.position.y + myTransform.right.y * _laserRange, myTransform.position.z);
myTransform.right = -myTransform.right;
timePassed = 0;
while (_size.x > _startSize.x)
{
timePassed += Time.deltaTime;
var linerT = timePassed / laserGrowTime;
_size.x = Mathf.Lerp(_laserRange, _startSize.x, linerT);
_capsuleSize.x = _size.x * _capsuleSizeMultiplier;
_capsuleOffset.x = (_size.x * _capsuleStartOffset.x) / _startSize.x;
_spriteRenderer.size = _size;
_capsuleCollider.size = _capsuleSize;
_capsuleCollider.offset = _capsuleOffset;
yield return null;
}
SpriteFade spriteFade = GetComponent<SpriteFade>();
if (spriteFade) StartCoroutine(spriteFade.SlowFadeRoutine());
else Destroy(gameObject);
}