forked from mixandjam/Batman-Arkham-Combat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic attack mechanic implementation
- Loading branch information
1 parent
def26ac
commit aad036a
Showing
142 changed files
with
354,669 additions
and
1,090 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.Events; | ||
using DG.Tweening; | ||
using Cinemachine; | ||
|
||
public class CombatScript : MonoBehaviour | ||
{ | ||
float hitAmount; | ||
Animator animator; | ||
EnemyDetection enemyDetection; | ||
MovementInput movementInput; | ||
|
||
private Transform lockedTarget; | ||
|
||
//Booleans | ||
public bool isAttackingEnemy = false; | ||
|
||
public Transform punchPosition; | ||
|
||
//Events | ||
public UnityEvent<Transform> OnHit; | ||
|
||
void Start() | ||
{ | ||
animator = GetComponent<Animator>(); | ||
enemyDetection = GetComponentInChildren<EnemyDetection>(); | ||
movementInput = GetComponent<MovementInput>(); | ||
} | ||
|
||
void Update() | ||
{ | ||
if (Input.GetKeyDown(KeyCode.E)) | ||
{ | ||
transform.DOMove(transform.position - transform.forward, .3f); | ||
animator.SetTrigger("Dodge"); | ||
} | ||
|
||
if (isAttackingEnemy) | ||
return; | ||
|
||
//Punch | ||
if (Input.GetKeyDown(KeyCode.Space)) | ||
{ | ||
if(enemyDetection.CurrentTarget() == null) | ||
{ | ||
Attack(null, 0); | ||
return; | ||
} | ||
|
||
//Lock target | ||
lockedTarget = enemyDetection.CurrentTarget(); | ||
|
||
//AttackTarget | ||
Attack(lockedTarget, TargetDistance(lockedTarget)); | ||
return; | ||
} | ||
|
||
} | ||
|
||
public void Attack(Transform target, float distance) | ||
{ | ||
if(target == null) | ||
AttackType("GroundPunch", .2f, null, 0); | ||
|
||
if(distance <= 4) | ||
AttackType("GroundPunch", .3f, target, .2f); | ||
|
||
if(distance > 4 && distance < 10) | ||
AttackType("AirPunch", .8f, target, .7f); | ||
|
||
if (GetComponent<CinemachineImpulseSource>()) | ||
GetComponent<CinemachineImpulseSource>().m_ImpulseDefinition.m_AmplitudeGain = 1 * distance; | ||
|
||
} | ||
|
||
void AttackType(string attackTrigger, float cooldown, Transform target, float movementDuration) | ||
{ | ||
animator.SetTrigger(attackTrigger); | ||
|
||
StopAllCoroutines(); | ||
StartCoroutine(MovementDisableCoroutine(cooldown)); | ||
|
||
if(target == null) | ||
return; | ||
|
||
MoveTorwardsTarget(target, movementDuration); | ||
|
||
IEnumerator MovementDisableCoroutine(float duration) | ||
{ | ||
isAttackingEnemy = true; | ||
movementInput.enabled = false; | ||
yield return new WaitForSeconds(duration); | ||
movementInput.enabled = true; | ||
isAttackingEnemy = false; | ||
LerpCharacterAcceleration(); | ||
} | ||
} | ||
|
||
void MoveTorwardsTarget(Transform target, float duration) | ||
{ | ||
transform.DOLookAt(target.position, .2f); | ||
transform.DOMove(TargetOffset(target), duration); | ||
} | ||
|
||
float TargetDistance(Transform target) | ||
{ | ||
return Vector3.Distance(transform.position, target.position); | ||
} | ||
|
||
public Vector3 TargetOffset(Transform target) | ||
{ | ||
Vector3 position; | ||
position = target.position; | ||
return Vector3.MoveTowards(position, transform.position, 1.05f); | ||
} | ||
|
||
public void HitEvent() | ||
{ | ||
if (lockedTarget == null) | ||
return; | ||
|
||
isAttackingEnemy = false; | ||
OnHit.Invoke(lockedTarget); | ||
FindObjectOfType<ParticleSystemScript>().PlayParticleAtPosition(punchPosition.position); | ||
} | ||
|
||
void LerpCharacterAcceleration() | ||
{ | ||
movementInput.acceleration = 0; | ||
DOVirtual.Float(0, 1, .6f, ((acceleration)=> movementInput.acceleration = acceleration)); | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class EnemyDetection : MonoBehaviour | ||
{ | ||
|
||
CombatScript combatScript; | ||
|
||
[Header("Targets in radius")] | ||
public List<Transform> targets; | ||
public int targetIndex; | ||
|
||
public LayerMask layerMask; | ||
|
||
Vector3 desiredMoveDirection; | ||
private Transform currentTarget; | ||
|
||
private void Start() | ||
{ | ||
combatScript = GetComponentInParent<CombatScript>(); | ||
} | ||
|
||
private void OnTriggerEnter(Collider other) | ||
{ | ||
if (other.CompareTag("Enemy")) | ||
{ | ||
targets.Add(other.transform); | ||
} | ||
} | ||
|
||
private void OnTriggerExit(Collider other) | ||
{ | ||
if (other.CompareTag("Enemy")) | ||
{ | ||
if (targets.Contains(other.transform)) | ||
targets.Remove(other.transform); | ||
} | ||
} | ||
|
||
private void Update() | ||
{ | ||
//Input | ||
float InputX = Input.GetAxis("Horizontal"); | ||
float InputZ = Input.GetAxis("Vertical"); | ||
|
||
var camera = Camera.main; | ||
var forward = camera.transform.forward; | ||
var right = camera.transform.right; | ||
|
||
forward.y = 0f; | ||
right.y = 0f; | ||
|
||
forward.Normalize(); | ||
right.Normalize(); | ||
|
||
desiredMoveDirection = forward * InputZ + right * InputX; | ||
desiredMoveDirection = desiredMoveDirection.normalized; | ||
|
||
RaycastHit info; | ||
|
||
if (Physics.SphereCast(transform.position, 1.5f, desiredMoveDirection,out info, 10,layerMask)) | ||
{ | ||
currentTarget = info.collider.transform; | ||
} | ||
} | ||
|
||
public Transform CurrentTarget() | ||
{ | ||
return currentTarget; | ||
} | ||
|
||
private void OnDrawGizmos() | ||
{ | ||
Gizmos.color = Color.black; | ||
Gizmos.DrawRay(transform.position, desiredMoveDirection); | ||
Gizmos.DrawWireSphere(transform.position, 1); | ||
if(CurrentTarget() != null) | ||
Gizmos.DrawSphere(CurrentTarget().position, .5f); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class EnemyScript : MonoBehaviour | ||
{ | ||
Animator animator; | ||
CombatScript playerCombat; | ||
|
||
void Start() | ||
{ | ||
animator = GetComponent<Animator>(); | ||
playerCombat = FindObjectOfType<CombatScript>(); | ||
playerCombat.OnHit.AddListener((x) => OnHit(x)); | ||
} | ||
|
||
void Update() | ||
{ | ||
transform.LookAt(new Vector3(playerCombat.transform.position.x, transform.position.y, playerCombat.transform.position.z)); | ||
} | ||
|
||
void OnHit(Transform target) | ||
{ | ||
if(transform == target) | ||
{ | ||
animator.SetTrigger("Hit"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.