//------------------------------- IEnumerator ShootProjecilet() { // Delay when pressing Shoot and being in the state where the animation is playing, to when the actual shoot is being spawned yield return(new WaitForSeconds(delayAfterPressingShootAndProjectileGetsShoot)); // Checks if still in the IsShooting state after the delay incase player has gotten hit or something if (playerState.IsState(PlayerStates.PossibleStates.IsShooting)) { Vector3 projectileSpawnLocation = (player.transform.position + player.transform.forward * lengthInFrontOfPlayerProjectileShouldSpawn); GameObject spawnedProjectile = Instantiate(projectileToSpawe, projectileSpawnLocation, player.gameObject.transform.rotation) as GameObject; spawnedProjectiles.Add(spawnedProjectile); spawnedProjectile.GetComponent <Projectile>().currentProjectileDamage = fireBallDamage; spawnedProjectile.GetComponent <Projectile>().minimumKnockback = fireBallKnockback; spawnedProjectile.GetComponent <Projectile>().minimumSpeed = fireBallSpeed; spawnedProjectile.GetComponent <Projectile>().startingTurnSpeed = fireBallStartingTurnSpeed; spawnedProjectile.GetComponent <Projectile>().targetTurnSpeed = fireBallTargetTurnSpeed; spawnedProjectile.GetComponent <Projectile>().timeToLerpFromStartingToTargetTurnSpeed = timeToLerpFromStartingToTargetTurnSpeed; GameObject spawnedProjectileVFX = Instantiate(projectileSpawnVFX, projectileSpawnLocation, player.transform.rotation) as GameObject; spawnedProjectile.GetComponent <Projectile>().owner = player; } // Delay after shooting and going back to normal yield return(new WaitForSeconds(delayAfterShootingProjectileAndGoingBackToNormal)); if (playerState.IsState(PlayerStates.PossibleStates.IsShooting)) { playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); } yield return(null); }
//----------------------------------- // Dash Lerp and Casts for hits IEnumerator Dash() { float leftJoystickInput; float initialDashVerticalAxis = playerMovement.verticalAxis; float initialDashHorizontalAxis = playerMovement.horizontalAxis; // Sets the leftJoystickInput to be the highest value that is coming from the left joystick if (Mathf.Abs(playerMovement.horizontalAxis) > Mathf.Abs(playerMovement.verticalAxis)) { leftJoystickInput = Mathf.Abs(playerMovement.horizontalAxis); } else { leftJoystickInput = Mathf.Abs(playerMovement.verticalAxis); } // Sets Length to dash depending on how much you press the left joystick. float lengthToDash = Mathf.Lerp(dashMinLength, dashMaxLength, leftJoystickInput); float dashDuration = Mathf.Lerp(dashMinDuration, dashMaxDuration, leftJoystickInput); float knockbackToApply = Mathf.Lerp(dashMinKnockback, dashMaxKnockback, leftJoystickInput); dashDuration = 1 / dashDuration; float dashCounter = 0; RaycastHit[] dashSphereCastHits; // Delay before starting dash, could be like 0.1 sek yield return(new WaitForSeconds(delayBeforeStartingDash)); // If Still in the Dash State after delay then continues with the coroutine, otherwise breaks it. if (playerState.IsState(PlayerStates.PossibleStates.IsDashing) == false) { yield break; } // Dash Start Location Vector3 dashStartLocation = gameObject.transform.position; // Dash Target Location Vector3 dashTargetLocation = (dashStartLocation + gameObject.transform.forward * lengthToDash); if (drawDebugDashTargetAndHitRadius) { Debug.DrawLine(dashStartLocation, dashTargetLocation, Color.red, 60); } // Spawns Particle Effects GameObject dashVFXTemp1 = Instantiate(dashVFX1, player.transform.position, Quaternion.identity) as GameObject; GameObject dashVFXTemp2 = Instantiate(dashVFX2, player.transform.position, Quaternion.identity) as GameObject; // Set Dash VFX Parent dashVFXTemp1.transform.SetParent(gameObject.transform.root.transform, true); dashVFXTemp2.transform.SetParent(gameObject.transform.root.transform, true); // While player is 0.1 meters away from the dash target location while (Vector3.Distance(player.transform.position, dashTargetLocation) > 0.1f) { Vector3 dirFromEnemyToPlayer; Vector3 dirFromPlayerToEnemy; // If Still Dashing then lerps the player if (playerState.IsState(PlayerStates.PossibleStates.IsDashing)) { if (useJoystickCancelDash) { // If players Move Left stick to much to another direction from where they started the dash then cancels it if (playerMovement.horizontalAxis >= initialDashHorizontalAxis + dashMoveCancelTreshHold || playerMovement.horizontalAxis <= initialDashHorizontalAxis - dashMoveCancelTreshHold) { playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); StopDash(); yield break; } // If players Move Left stick to much to another direction from where they started the dash then cancels it else if (playerMovement.verticalAxis >= initialDashVerticalAxis + dashMoveCancelTreshHold || playerMovement.verticalAxis <= initialDashVerticalAxis - dashMoveCancelTreshHold) { playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); StopDash(); yield break; } } // Lerps the player toward from start locaation to dash location, casting for hits along the way. player.transform.position = Vector3.Lerp(dashStartLocation, dashTargetLocation, dashSpeedCurve.Evaluate(dashCounter)); dashCounter += dashDuration * Time.deltaTime; // Sphere cast for enemies and obstacles, if 2 hit eachother and are both dashing then both should get stunned. dashSphereCastHits = Physics.SphereCastAll(player.transform.position, dashHitRadius, gameObject.transform.forward, dashHitRadius, dashTraceFor); // If got a Hit when dashing then lerps through them and checks what it was if (dashSphereCastHits.Length > 0) { foreach (RaycastHit dashHits in dashSphereCastHits) { if (dashHits.transform.gameObject.tag == "Player" && dashHits.transform.gameObject != player) { // If Dashing into Enemy that is also dashing if (dashHits.transform.gameObject.GetComponent <PlayerStates>().IsState(PlayerStates.PossibleStates.IsDashing) && !enemiesDashedInto.Contains(dashHits.transform.gameObject)) { enemiesDashedInto.Add(dashHits.transform.gameObject); // Calls Hit on Self when dashing into eachother dirFromEnemyToPlayer = (transform.position - dashHits.collider.gameObject.transform.position).normalized; player.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromEnemyToPlayer.x, knockbackY, dirFromEnemyToPlayer.z), knockbackWhenDashingIntoEachother, dashDamage, dashOnDashCameraShakeDuration, dashOnDashCameraShakeIntensity, PlayerHit.HitBy.Dash); // Calls Hit on Enemy when dashing into eachother dirFromPlayerToEnemy = (dashHits.collider.gameObject.transform.position - transform.position).normalized; dashHits.collider.gameObject.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromPlayerToEnemy.x, knockbackY, dirFromPlayerToEnemy.z), knockbackWhenDashingIntoEachother, dashDamage, 0, 0, PlayerHit.HitBy.Dash); // Get Middle point between two players that have dashed into eachother Vector3 locationBetweenPlayers = (transform.position + dashHits.collider.gameObject.transform.position) / 2; // Spawn dashingIntoEachotherFX in that location GameObject spawnedProjectileFX = Instantiate(dashOnDashVFX, locationBetweenPlayers, Quaternion.identity) as GameObject; timeManager.SlowDownTime(dashOnDashSlowDownTimeScale, dashOnDashSlowDownDuration); } // If Dashing into player that is not dashing else if (!dashHits.transform.gameObject.GetComponent <PlayerStates>().IsState(PlayerStates.PossibleStates.IsDashing) && !enemiesDashedInto.Contains(dashHits.transform.gameObject)) { enemiesDashedInto.Add(dashHits.transform.gameObject); // Get Direction Vector Hit Player and Calls Hit dirFromPlayerToEnemy = (dashHits.collider.gameObject.transform.position - transform.position).normalized; dashHits.collider.gameObject.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromPlayerToEnemy.x, knockbackY, dirFromPlayerToEnemy.z), knockbackToApply, dashDamage, 0, 0, PlayerHit.HitBy.Dash); // Spawns the Dash VFX this player uses on the enemy that it hit GameObject enemyDashedIntoVFXTemp1 = Instantiate(dashVFX1, dashHits.transform.gameObject.transform.position, Quaternion.identity) as GameObject; GameObject enemyDashedIntoVFXTemp2 = Instantiate(dashVFX2, dashHits.transform.gameObject.transform.position, Quaternion.identity) as GameObject; // Set Dash VFX Parent enemyDashedIntoVFXTemp1.transform.SetParent(dashHits.transform.gameObject.transform, true); enemyDashedIntoVFXTemp2.transform.SetParent(dashHits.transform.gameObject.transform, true); // TODO If only 2 players left, always slow down time when hitting with dash // timeManager.SlowDownTime(dashSlowDownTimeScale, dashSlowDownDuration); } } // If Dashed into Wall else if (dashHits.transform.gameObject.tag == "Wall") { // If Hit Wall then returns to Normal State and Stops Dash playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); StopDash(); yield break; } } } // If no hit and still in Dash State then returns null so while loop runs again yield return(null); } // If No longer in Dashing state then Stops Dash without changing State back to normal else { StopDash(); yield break; } } StopDash(); // If Dash Complete, can have delay DashEndTime before going back to Normal yield return(new WaitForSeconds(delayAfterDashBeforeReturningToNormal)); // If still in dash state after delay then sets it to be normal, otherwise just returns null if (playerState.IsState(PlayerStates.PossibleStates.IsDashing)) { playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); } yield return(null); }
//----------------------------------- IEnumerator Reflecting() { float counter = 0; float scaleRadiusCounter = 1 / reflectDuration; RaycastHit[] reflectCastHits; // float reflectMoveForwardCounter = 0; // RaycastHit[] reflectMoveForwardCastHits; // Plays Reflect FX reflectFXParticle.Play(true); // Small Delay After FX Started and Collision is Actually Enabled. Good if it takes a few frames for FX to kick in. yield return(new WaitForSeconds(delayFromVFXSpawnToReflectStart)); // Check is still reflecting, if not then returns. Exchange for real state later if (!playerState.IsState(PlayerStates.PossibleStates.IsReflecting)) { yield break; } /* Related to Reflect Move Forward which is not in use anymore * // Reflect Start Location * Vector3 reflectStartMoveLocation = player.transform.gameObject.transform.position; * * // Reflect Target Location * Vector3 reflectTargetMoveLocation = (reflectStartMoveLocation + player.transform.gameObject.transform.forward * reflectLengthToMoveForward); */ while (counter <= reflectDuration) { if (playerState.IsState(PlayerStates.PossibleStates.IsReflecting)) { reflectRadius = Mathf.Lerp(reflectRadiusFrom, reflectRadiusTo, counter * scaleRadiusCounter); counter += Time.deltaTime; // Sphere cast for enemies and obstacles, if 2 hit eachother and are both dashing then both should get stunned. reflectCastHits = Physics.SphereCastAll(player.transform.position, reflectRadius, gameObject.transform.forward, reflectRadius, reflectTraceFor); if (reflectCastHits.Length > 0) { foreach (RaycastHit reflectHits in reflectCastHits) { // Calculates the Angle Hit Vector3 direction = reflectHits.transform.gameObject.transform.root.gameObject.transform.position - transform.root.transform.position; float angle = Vector3.Angle(direction, gameObject.transform.forward); // If Collide with Player if (reflectHits.transform.gameObject.tag == "Player" && reflectHits.transform.gameObject != player && !playersReflected.Contains(reflectHits.transform.gameObject) && angle < reflectHitAngle && !reflectHits.transform.gameObject.GetComponent <PlayerStates>().IsState(PlayerStates.PossibleStates.IsReflecting)) { playersReflected.Add(reflectHits.transform.gameObject); Vector3 dirFromPlayerToEnemy = (reflectHits.transform.gameObject.transform.position - transform.position).normalized; // Spawns VFX when reflected player GameObject reflectedPlayerTemp = Instantiate(playerHitWithReflectVFX, reflectHits.transform.gameObject.transform.position, Quaternion.identity) as GameObject; // Call Hit On Play with 0 damage and Knockback reflectHits.transform.gameObject.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromPlayerToEnemy.x, reflectKnockbackY, dirFromPlayerToEnemy.z), reflectKnockback, reflectDamage, 0, 0, PlayerHit.HitBy.Reflect); } // If Two Reflects Hit Eachother else if (reflectHits.transform.gameObject.tag == "Player" && reflectHits.transform.gameObject != player && !playersReflected.Contains(reflectHits.transform.gameObject) && angle < reflectHitAngle && reflectHits.transform.gameObject.GetComponent <PlayerStates>().IsState(PlayerStates.PossibleStates.IsReflecting)) { // TODO - Adda Reflect On Reflect VFX Here playersReflected.Add(reflectHits.transform.gameObject); Vector3 dirFromEnemyToPlayer = (transform.position - reflectHits.transform.gameObject.transform.root.gameObject.transform.position).normalized; Vector3 dirFromPlayerToEnemy = (reflectHits.transform.gameObject.transform.root.gameObject.transform.position - transform.position).normalized; reflectHits.transform.gameObject.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromPlayerToEnemy.x, reflectKnockbackY, dirFromPlayerToEnemy.z), reflectOnReflectKnockback, reflectDamage, 0.1f, 0.05f, PlayerHit.HitBy.Reflect); player.GetComponent <PlayerHit>().OnPlayerHit(new Vector3(dirFromEnemyToPlayer.x, reflectKnockbackY, dirFromEnemyToPlayer.z), reflectOnReflectKnockback, reflectDamage, 0.1f, 0.05f, PlayerHit.HitBy.Reflect); timeManager.SlowDownTime(reflectOnReflectSlowDownTimeScale, reflectOnReflectSlowDownDuration); } // If Reflect Hit a Projectile and its not the players own projectile else if (reflectHits.transform.gameObject.tag == "Projectile" && !player.GetComponentInChildren <ProjectileAbility>().spawnedProjectiles.Contains(reflectHits.transform.gameObject.transform.root.gameObject) && angle < reflectHitAngle) { // Calls Got Reflected on the Projectile reflectHits.transform.gameObject.GetComponent <Projectile>().GotReflected(transform.root.gameObject, reflectHits.point); ReflectedProjectile(reflectHits.point); } } } /* Related to Reflect Move Forward which is not in use anymore * * if (ShouldMoveForwardWhenReflecting) { * // Moves player forward a bit when reflecting * player.transform.position = Vector3.Lerp(reflectStartMoveLocation, reflectTargetMoveLocation, reflectMoveForwardCounter); * * reflectMoveForwardCastHits = Physics.SphereCastAll(player.transform.position, reflectMoveForwardCastRadius, gameObject.transform.forward, reflectMoveForwardCastRadius, reflectMoveForwardTraceFor); * * // If not hitting Wall, then lerps forward * if (reflectMoveForwardCastHits.Length == 0) { * * reflectMoveForwardCounter = counter * reflectSpeedToMoveForward; * } * } */ // When reflected a Projectile if (haveReflectedProjectile) { // Stops Emitting the FX, making it dissapear faster reflectFXParticle.Stop(true, ParticleSystemStopBehavior.StopEmitting); haveReflectedProjectile = false; // Small delay for when VFX is gone yield return(new WaitForSeconds(delayAfterVFXDissapearingAndReflectOff)); playersReflected.Clear(); reflectRadius = reflectRadiusFrom; // If still IsReflecting after the small delay, then resets it to normal if (playerState.IsState(PlayerStates.PossibleStates.IsReflecting)) { playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); } yield break; } } else { reflectFXParticle.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear); playersReflected.Clear(); reflectRadius = reflectRadiusFrom; yield break; } yield return(null); } // If Reflect is Finished without get abrubted then sets state to Normal playerState.SetStateTo(PlayerStates.PossibleStates.IsNormal); playersReflected.Clear(); reflectRadius = reflectRadiusFrom; }