New enemy movement, enemy spawner, player movement (w/ particle effects)

This commit is contained in:
Arlo Filley 2024-11-24 01:34:56 +00:00
parent 3d763eef0b
commit b5c039edd3
7 changed files with 5128 additions and 1091 deletions

BIN
Assets/Models/Spider.fbx Normal file

Binary file not shown.

View File

@ -0,0 +1,107 @@
fileFormatVersion: 2
guid: 811bf8368ec5efe43bcc1c0b90ec0c99
ModelImporter:
serializedVersion: 21300
internalIDToNameTable: []
externalObjects: {}
materials:
materialImportMode: 2
materialName: 0
materialSearch: 1
materialLocation: 1
animations:
legacyGenerateAnimations: 4
bakeSimulation: 0
resampleCurves: 1
optimizeGameObjects: 0
removeConstantScaleCurves: 1
motionNodeName:
rigImportErrors:
rigImportWarnings:
animationImportErrors:
animationImportWarnings:
animationRetargetingWarnings:
animationDoRetargetingWarnings: 0
importAnimatedCustomProperties: 0
importConstraints: 0
animationCompression: 1
animationRotationError: 0.5
animationPositionError: 0.5
animationScaleError: 0.5
animationWrapMode: 0
extraExposedTransformPaths: []
extraUserProperties: []
clipAnimations: []
isReadable: 0
meshes:
lODScreenPercentages: []
globalScale: 1
meshCompression: 0
addColliders: 0
useSRGBMaterialColor: 1
sortHierarchyByName: 1
importVisibility: 1
importBlendShapes: 1
importCameras: 1
importLights: 1
nodeNameCollisionStrategy: 1
fileIdsGeneration: 2
swapUVChannels: 0
generateSecondaryUV: 0
useFileUnits: 1
keepQuads: 0
weldVertices: 1
bakeAxisConversion: 0
preserveHierarchy: 0
skinWeightsMode: 0
maxBonesPerVertex: 4
minBoneWeight: 0.001
optimizeBones: 1
meshOptimizationFlags: -1
indexFormat: 0
secondaryUVAngleDistortion: 8
secondaryUVAreaDistortion: 15.000001
secondaryUVHardAngle: 88
secondaryUVMarginMethod: 1
secondaryUVMinLightmapResolution: 40
secondaryUVMinObjectScale: 1
secondaryUVPackMargin: 4
useFileScale: 1
strictVertexDataChecks: 0
tangentSpace:
normalSmoothAngle: 60
normalImportMode: 0
tangentImportMode: 3
normalCalculationMode: 4
legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 0
blendShapeNormalImportMode: 1
normalSmoothingSource: 0
referencedClips: []
importAnimation: 1
humanDescription:
serializedVersion: 3
human: []
skeleton: []
armTwist: 0.5
foreArmTwist: 0.5
upperLegTwist: 0.5
legTwist: 0.5
armStretch: 0.05
legStretch: 0.05
feetSpacing: 0
globalScale: 1
rootMotionBoneName:
hasTranslationDoF: 0
hasExtraRoot: 0
skeletonHasParents: 1
lastHumanDescriptionAvatarSource: {instanceID: 0}
autoGenerateAvatarMappingIfUnspecified: 1
animationType: 2
humanoidOversampling: 1
avatarSetup: 0
addHumanoidExtraRootOnlyWhenUsingAvatar: 1
remapMaterialsIfMaterialImportModeIsNone: 0
additionalBone: 0
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -4,21 +4,80 @@ using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
public enum MovementTypes
{
Line,
Circle,
ThroughPlayer,
Follow,
}
public class EnemyMovement : MonoBehaviour public class EnemyMovement : MonoBehaviour
{ {
[Header("Speed")] [Header("Speed")]
public float speed = 2; public float speed = 2;
public float range = 4; public float range = 4;
public MovementTypes movementType = MovementTypes.Circle;
public Transform enemy; public Transform enemy;
public Transform player;
public Vector3 currentLookDirection = new Vector3(0f, 0f, 0f);
public float timeTillNewLookDirection = 0.5f;
public Vector3 playerStartPoint;
[Header("Spawn")]
public float radius = 5f;
public Vector3 center = new Vector3(0f, 0f, 0f);
void Start() void Start()
{ {
float randomAngle = UnityEngine.Random.Range(0f, 2 * Mathf.PI);
// Calculate the point on the perimeter of the circle
Vector3 point = center + new Vector3(Mathf.Cos(randomAngle) * radius, 0.6f, Mathf.Sin(randomAngle) * radius);
enemy.position = point;
if (movementType == MovementTypes.ThroughPlayer)
{
enemy.LookAt(new Vector3(player.position.x, enemy.position.y, player.position.z));
enemy.Rotate(180.0f, 0.0f, 180.0f, Space.World);
}
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
enemy.position = new Vector3(Convert.ToSingle(Math.Cos(Time.time * speed)) * range, 0, Convert.ToSingle(Math.Sin(Time.time * speed)) * range); if (movementType == MovementTypes.Line)
{
enemy.position = new Vector3(Convert.ToSingle(Math.Cos(Time.time * speed)) * range, enemy.position.y, Time.deltaTime * speed);
}
else if (movementType == MovementTypes.Circle)
{
enemy.position = new Vector3(Convert.ToSingle(Math.Cos(Time.time * speed)) * range, enemy.position.y, Convert.ToSingle(Math.Sin(Time.time * speed)) * range);
}
else if (movementType == MovementTypes.ThroughPlayer)
{
enemy.position -= Time.deltaTime * enemy.forward * speed;
}
else if (movementType == MovementTypes.Follow)
{
enemy.position -= Time.deltaTime * enemy.forward * speed;
Vector3 newlookDirection = new Vector3(player.position.x, enemy.position.y, player.position.z);
// if (timeTillNewLookDirection <= 0) {
// newlookDirection = new Vector3(player.position.x, enemy.position.y, player.position.z);
// timeTillNewLookDirection = 0.2f;
// } else {
// newlookDirection = currentLookDirection;
// }
timeTillNewLookDirection -= Time.deltaTime;
enemy.LookAt(Vector3.Slerp(currentLookDirection, newlookDirection, 0.1f / (100000 * 10000)));
enemy.Rotate(180.0f, 0.0f, 180.0f, Space.World);
currentLookDirection = newlookDirection;
}
} }
} }

View File

@ -0,0 +1,44 @@
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemyPrefab; // Reference to the enemy prefab
public float spawnInterval = 3f; // Time between each spawn (in seconds)
public float enemyLifetime = 5f; // Time before the enemy is destroyed (in seconds)
public Vector3 spawnPosition = new Vector3(0f, 0.6f, 0f); // Position to spawn enemies
void Start()
{
// Start the coroutine to spawn enemies at intervals
StartCoroutine(SpawnEnemies());
}
IEnumerator SpawnEnemies()
{
// Infinite loop to keep spawning enemies at intervals
while (true)
{
SpawnEnemy(); // Call method to spawn an enemy
yield return new WaitForSeconds(spawnInterval); // Wait for the spawn interval before spawning the next enemy
}
}
void SpawnEnemy()
{
// Instantiate the enemy at the spawn position
GameObject newEnemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);
// Start a coroutine to destroy the enemy after a set time
StartCoroutine(DestroyEnemyAfterTime(newEnemy, enemyLifetime));
}
IEnumerator DestroyEnemyAfterTime(GameObject enemy, float time)
{
// Wait for the specified time
yield return new WaitForSeconds(time);
// Destroy the enemy after the time has passed
Destroy(enemy);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4034626f7ccadd840a8984e642c7feb3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -16,6 +16,7 @@ public class PlayerController : MonoBehaviour
[Header("Speed")] [Header("Speed")]
public float speed = 5; public float speed = 5;
public float curSpeed = 0f; public float curSpeed = 0f;
public bool canMove = true;
[Header("Dash")] [Header("Dash")]
public float dashSpeed = 10; public float dashSpeed = 10;
@ -25,6 +26,11 @@ public class PlayerController : MonoBehaviour
public bool canDash = true; public bool canDash = true;
public float dashCooldownTimer = 0f; public float dashCooldownTimer = 0f;
[Header("Particles")]
public ParticleSystem particleSystem;
public Transform particleTransform;
[Header("Lives")] [Header("Lives")]
public string enemyTag = "Enemy"; public string enemyTag = "Enemy";
public float invulnerabilityTimeSeconds = 0.25f; public float invulnerabilityTimeSeconds = 0.25f;
@ -58,44 +64,37 @@ public class PlayerController : MonoBehaviour
if (Input.GetKey(dashKey) && canDash) if (Input.GetKey(dashKey) && canDash)
{ {
StartCoroutine(DashCooldown()); // Start Dash Coroutine when dash is pressed StartCoroutine(Dash());
curSpeed = dashSpeed; }
else if (canMove)
{
rb.velocity = new Vector3( rb.velocity = new Vector3(
moveDirection.x * curSpeed, moveDirection.x * curSpeed,
rb.velocity.y, rb.velocity.y,
moveDirection.z * curSpeed moveDirection.z * curSpeed
); );
} }
else if (Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2) < speed)
{
rb.AddForce(new Vector3 (moveDirection.x * curSpeed, 0, moveDirection.z * curSpeed), ForceMode.Force);
}
} }
void HandleDash() IEnumerator Dash()
{ {
canMove = false;
}
IEnumerator DashCooldown()
{
// Start dash
canDash = false; canDash = false;
Vector3 dashDirection = GetPlayerDirection().normalized; Vector3 dashDirection = GetPlayerDirection().normalized;
rb.velocity = dashDirection * dashSpeed; // Apply dash velocity rb.velocity = dashDirection * dashSpeed;
particleSystem.Play();
particleTransform.SetLocalPositionAndRotation(dashDirection * -1.5f, particleTransform.localRotation);
// Wait for the dash to finish
yield return new WaitForSeconds(dashDuration); yield return new WaitForSeconds(dashDuration);
// End dash: reset velocity and start cooldown particleSystem.Stop();
rb.velocity = Vector3.zero; // Optionally reset velocity after dash (for more control)
dashCooldownTimer = dashCooldown; rb.velocity = Vector3.zero;
canMove = true;
// Wait for cooldown
yield return new WaitForSeconds(dashCooldown); yield return new WaitForSeconds(dashCooldown);
// Allow next dash
canDash = true; canDash = true;
} }