CUBE DASH

This commit is contained in:
Arlo Filley 2024-11-22 22:58:34 +00:00
parent 373211a586
commit 3d763eef0b
4 changed files with 201 additions and 37 deletions

View File

@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1 m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0} m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994} m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.18028326, g: 0.22571333, b: 0.30692202, a: 1} m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
m_UseRadianceAmbientProbe: 0 m_UseRadianceAmbientProbe: 0
--- !u!157 &3 --- !u!157 &3
LightmapSettings: LightmapSettings:
@ -564,12 +564,19 @@ MonoBehaviour:
backwardKey: 111 backwardKey: 111
leftKey: 97 leftKey: 97
rightKey: 101 rightKey: 101
speed: 5 speed: 10
moveDirection: {x: 0, y: 0, z: 0} curSpeed: 0
rb: {fileID: 1261174834} dashSpeed: 20
dashDuration: 0.3
dashCooldown: 2
dashKey: 304
canDash: 1
dashCooldownTimer: 0
enemyTag: Enemy
invulnerabilityTimeSeconds: 1.92 invulnerabilityTimeSeconds: 1.92
lastHit: 0 lives: 3
metalPipe: {fileID: 932053744} metalPipe: {fileID: 932053744}
rb: {fileID: 1261174834}
--- !u!65 &1261174830 --- !u!65 &1261174830
BoxCollider: BoxCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -658,7 +665,7 @@ Rigidbody:
m_GameObject: {fileID: 1261174828} m_GameObject: {fileID: 1261174828}
serializedVersion: 2 serializedVersion: 2
m_Mass: 1 m_Mass: 1
m_Drag: 0 m_Drag: 2
m_AngularDrag: 0.05 m_AngularDrag: 0.05
m_UseGravity: 1 m_UseGravity: 1
m_IsKinematic: 0 m_IsKinematic: 0

View File

@ -1,10 +1,10 @@
using UnityEngine; using UnityEngine;
using System.Collections;
using UnityEngine.UI; using UnityEngine.UI;
#if UNITY_EDITOR #if UNITY_EDITOR
using UnityEditor; using UnityEditor;
#endif #endif
public class PlayerController : MonoBehaviour public class PlayerController : MonoBehaviour
{ {
[Header("Keybinds")] [Header("Keybinds")]
@ -15,31 +15,31 @@ public class PlayerController : MonoBehaviour
[Header("Speed")] [Header("Speed")]
public float speed = 5; public float speed = 5;
public float curSpeed = 0f;
[Header("Dash")]
public float dashSpeed = 10;
public float dashDuration = 0.3f; // Dash duration
public float dashCooldown = 2f;
public KeyCode dashKey = KeyCode.LeftShift;
public bool canDash = true;
public float dashCooldownTimer = 0f;
[Header("Lives")] [Header("Lives")]
public string enemyTag = "Enemy"; public string enemyTag = "Enemy";
public float invulnerabilityTimeSeconds = 0.25f; public float invulnerabilityTimeSeconds = 0.25f;
public float lastHit = 0;
public int lives = 3; public int lives = 3;
public GameObject[] hearts;
[Header("Audio")] [Header("Audio")]
public AudioSource metalPipe; public AudioSource metalPipe;
[Header("Player Object")] [Header("Player Object")]
public Vector3 moveDirection;
public Rigidbody rb; public Rigidbody rb;
private float lastHit = 0f;
void Start() void Start()
{ {
if (hearts.Length != 3)
{
Debug.LogError("Hearts array not properly configured. Expected length 3.");
enabled = false;
return;
}
if (!TryGetComponent(out rb)) if (!TryGetComponent(out rb))
{ {
Debug.LogError("Rigidbody not found!"); Debug.LogError("Rigidbody not found!");
@ -53,37 +53,87 @@ public class PlayerController : MonoBehaviour
void Update() void Update()
{ {
moveDirection = PlayerInput(); curSpeed = speed;
rb.velocity = moveDirection.normalized * speed; Vector3 moveDirection = GetPlayerDirection().normalized;
}
Vector3 PlayerInput() if (Input.GetKey(dashKey) && canDash)
{ {
Vector3 direction = Vector3.zero; StartCoroutine(DashCooldown()); // Start Dash Coroutine when dash is pressed
curSpeed = dashSpeed;
if (Input.GetKey(rightKey)) direction.x += 1f; rb.velocity = new Vector3(
if (Input.GetKey(leftKey)) direction.x -= 1f; moveDirection.x * curSpeed,
if (Input.GetKey(forwardKey)) direction.z += 1f; rb.velocity.y,
if (Input.GetKey(backwardKey)) direction.z -= 1f; moveDirection.z * curSpeed
);
return direction; }
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 DashCooldown()
{
// Start dash
canDash = false;
Vector3 dashDirection = GetPlayerDirection().normalized;
rb.velocity = dashDirection * dashSpeed; // Apply dash velocity
// Wait for the dash to finish
yield return new WaitForSeconds(dashDuration);
// End dash: reset velocity and start cooldown
rb.velocity = Vector3.zero; // Optionally reset velocity after dash (for more control)
dashCooldownTimer = dashCooldown;
// Wait for cooldown
yield return new WaitForSeconds(dashCooldown);
// Allow next dash
canDash = true;
}
Vector3 GetPlayerDirection()
{
float horizontal = 0f;
float vertical = 0f;
if (Input.GetKey(rightKey)) horizontal += 1f;
if (Input.GetKey(leftKey)) horizontal -= 1f;
if (Input.GetKey(forwardKey)) vertical += 1f;
if (Input.GetKey(backwardKey)) vertical -= 1f;
return new Vector3(horizontal, 0, vertical);
}
void OnCollisionEnter(Collision collision) void OnCollisionEnter(Collision collision)
{ {
bool collidedWithEnemy = collision.gameObject.tag == enemyTag; bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag);
bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit; bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit;
if (collidedWithEnemy && !isInvulnerable) if (collidedWithEnemy && !isInvulnerable)
{
HandlePlayerHit();
}
}
void HandlePlayerHit()
{ {
lastHit = Time.time; lastHit = Time.time;
metalPipe.Play(); metalPipe.Play(); // Play damage sound effect
lives--; lives--;
hearts[lives].GetComponent<RawImage>().color = Color.gray; // Update visual feedback (if any) for losing a life, e.g. hearts
bool playerOutOfLives = lives == 0; if (lives <= 0)
if (playerOutOfLives) ExitGame(); {
ExitGame();
} }
} }

BIN
Assets/untitled.fbx Normal file

Binary file not shown.

107
Assets/untitled.fbx.meta Normal file
View File

@ -0,0 +1,107 @@
fileFormatVersion: 2
guid: 902a72da88f601742b5c96bce99e71a7
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: