Hi,
Im not really that experienced so i would need a help with a part of a project where im stuck
Im working on a 2d game where player will have multiple attack types available, for example melee attack1 and attack2, ranged attack1 and attack2. Attacks would have different damage and some other properites, same as animations. Im thinking scriptable objects are way to do different attack types?
Under player object i have childed empty game object that is having box collider that is active only at the time of attack. Ranged attacks are making projectiles that are having their own collider.
My biggest issue here is how to detect which attack type was active during collision with enemy?
Scriptable object:
public class AttackTypes : ScriptableObject
{
[SerializeField] private int attackDamage;
[SerializeField] private int manaCostUsage;
[SerializeField] private bool isAttackAvailable;
}
Attack script thats put on a player object:
public class PlayerAttack : MonoBehaviour
{
[SerializeField] private Transform projectileSpawnLocation;
[SerializeField] private GameObject projectilePrefab;
[SerializeField] private AttackTypes[] attackTypes;
private Animator animator;
private BoxCollider2D boxCollider;
private float facingDirectionAtTimeOfAttack;
private bool isPlayerUsingRangedAttack = false;
public float FacingDirectionAtTimeOfAttack
{
get
{
return facingDirectionAtTimeOfAttack;
}
}
private void Awake()
{
animator = GetComponent<Animator>();
boxCollider = GetComponentInChildren<BoxCollider2D>();
boxCollider.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(Attack("Attack"));
}
else if (Input.GetKeyDown(KeyCode.R))
{
isPlayerUsingRangedAttack = true;
StartCoroutine(Attack("CastAttack"));
}
}
private IEnumerator Attack(string attackName)
{
animator.SetTrigger(attackName);
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
if (isPlayerUsingRangedAttack)
{
Vector3 newSpawnPosition = projectileSpawnLocation.position;
Quaternion newRotationForPosition = projectileSpawnLocation.rotation;
facingDirectionAtTimeOfAttack = transform.localScale.x;
Instantiate(projectilePrefab, newSpawnPosition, newRotationForPosition);
isPlayerUsingRangedAttack = false;
}
}
private void AddColliderAnimationEvent()
{
boxCollider.enabled = true;
}
private void RemoveColliderAnimationEvent()
{
boxCollider.enabled = false;
}
}
Script added to object thats childed to player and having only box collider:
public class PlayerAttackCollision : MonoBehaviour
{
[SerializeField] private AttackTypes attackType;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
//detect which attack type was used and take its stats
}
}
}