Skip to content

Commit

Permalink
basic attack mechanic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cardosoandre committed Jul 3, 2021
1 parent def26ac commit aad036a
Show file tree
Hide file tree
Showing 142 changed files with 354,669 additions and 1,090 deletions.
1 change: 0 additions & 1 deletion Assets/Art/Materials/General/Aluminium_Mat.mat
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AORemapMax: 1
- _AORemapMin: 0
Expand Down
135 changes: 135 additions & 0 deletions Assets/CombatScript.cs
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));
}

}
11 changes: 11 additions & 0 deletions Assets/CombatScript.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions Assets/EnemyDetection.cs
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);
}
}
11 changes: 11 additions & 0 deletions Assets/EnemyDetection.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Assets/EnemyScript.cs
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");
}
}
}
11 changes: 11 additions & 0 deletions Assets/EnemyScript.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aad036a

Please sign in to comment.