private void HandleBulletHitDetected(Vector2 position, Vector2 direction) { if (_isDying) { return; } _energy--; if (_energyVariable) { _energyVariable.value = _energy; } if (wasHitEvent != null) { wasHitEvent(); } _wasHitEvent.position = position; _wasHitEvent.direction = direction; _wasHitEvent.Raise(this, _wasHitEvent); if (_energy <= 0) { _wasKilledEvent.position = transform.position; _wasKilledEvent.direction = direction; _wasKilledEvent.Raise(this, _wasKilledEvent); _isDying = true; if (didDieEvent != null) { didDieEvent(); } StartCoroutine(DyingCoroutine(direction)); } }
void OnTriggerEnter2D(Collider2D other) { _bulletDidHitObstacleEvent.position = _rigidbody.position; _bulletDidHitObstacleEvent.direction = _direction; _bulletDidHitObstacleEvent.Raise(this, _bulletDidHitObstacleEvent); this.Recycle(); }
private IEnumerator DyingCoroutine(Vector2 direction) { foreach (var collider in _colliders) { collider.enabled = false; } _enemyMovementAI.enabled = false; Vector3 startPos = transform.position; var yieldInstruction = new WaitForEndOfFrame(); float directionMul = direction.x > 0 ? 1.0f : -1.0f; float jumpLength = 3.0f; float duration = 0.6f; float elapsedTime = 0.0f; float rotation = 320.0f; while (elapsedTime < duration) { float t = elapsedTime / duration; transform.position = startPos + new Vector3(directionMul * t * jumpLength, jumpLength * _deathJumpCurve.Evaluate(t), 0.0f); transform.eulerAngles = new Vector3(0.0f, 0.0f, rotation * t); elapsedTime += Time.deltaTime; yield return(yieldInstruction); } transform.eulerAngles = Vector3.zero; _didFinishDeathJump.position = transform.position; _didFinishDeathJump.Raise(this, _didFinishDeathJump); this.Recycle(); }
private void Update() { _timeAccumulator += Time.deltaTime; var fireButtonActive = false; if (_useKeyDown) { fireButtonActive = Input.GetKeyDown(KeyCode.X); } else { fireButtonActive = Input.GetKey(KeyCode.X); } if (fireButtonActive && _timeAccumulator >= _interval) { var firePointPos = _firePoint.transform.position; float flipDirection = _characterMovementController.movingDirection == CharacterMovementController.MovingDirection.Left ? 1.0f : -1.0f; var bullet = _bulletPrefab.Spawn(firePointPos); var direction = -_firePoint.transform.right * flipDirection; bullet.Init(direction); _timeAccumulator -= _interval; _characterMovementController.PushBack(_kickBack); _didFireEvent.position = firePointPos; _didFireEvent.direction = direction; _didFireEvent.Raise(this, _didFireEvent); } if (_timeAccumulator > _interval) { _timeAccumulator = _interval; } }
private void FixedUpdate() { Vector3 velocity = _rigidbody.velocity; Vector3 position = _rigidbody.position; _position.value = position; if (_canBeGroundedTimer > 0.0f) { _canBeGroundedTimer -= Time.fixedDeltaTime; } var isTouchingGround = Physics2D.BoxCastNonAlloc((Vector2)position + _groundCheckOffset, _groundCheckSize, 0.0f, Vector2.down, _raycastResults, _groundCheckDistance, _groundCheckLayerMask) > 0; // Ground Check if (_canBeGroundedTimer <= 0.0f && isTouchingGround) { _grounded = true; if (!_wasTouchingGround) { _didBecomeGrounded.position = transform.position; _didBecomeGrounded.Raise(this, _didBecomeGrounded); } } _wasTouchingGround = isTouchingGround; // else { // _grounded = false; // } bool directionKeyIsActive = false; bool leftKeyActive = Input.GetKey(KeyCode.LeftArrow); bool rightKeyActive = Input.GetKey(KeyCode.RightArrow); bool jumpKeyActive = Input.GetKey(KeyCode.Z); // Controls if (leftKeyActive ^ rightKeyActive) { if (leftKeyActive) { _movingDirection = MovingDirection.Left; directionKeyIsActive = true; if (velocity.x > 0.0f) { velocity.x *= _groundFriction; } velocity.x -= Time.fixedDeltaTime * _movementSpeed; } if (rightKeyActive) { _movingDirection = MovingDirection.Right; directionKeyIsActive = true; if (velocity.x < 0.0f) { velocity.x *= _groundFriction; } velocity.x += Time.fixedDeltaTime * _movementSpeed; } } if (!directionKeyIsActive) { velocity.x *= _grounded ? _groundFriction : _airFriction; velocity += _pushAccumulator; _pushAccumulator = Vector3.zero; } // Max velocity if (velocity.x > _maxVerticalSpeed) { velocity.x = _maxVerticalSpeed; } else if (velocity.x < -_maxVerticalSpeed) { velocity.x = -_maxVerticalSpeed; } // Jump if (jumpKeyActive) { if (_grounded && _jumpKeyWasUp) { velocity.y = _jumpStartSpeed; _canBeGroundedTimer = _minTimeBetweenGrounded; _jumpEnergy = _jumpStartEnergy; _grounded = false; _jumpKeyWasUp = false; _characterStartedJumpEvent.position = transform.position; _characterStartedJumpEvent.Raise(this, _characterStartedJumpEvent); } else { velocity.y += _jumpEnergy * Time.fixedDeltaTime; } } else { _jumpKeyWasUp = true; } _jumpEnergy *= _jumpEnergyDepletionMul; _rigidbody.velocity = velocity; }