The question is answered, right answer was accepted
Beginner with unity. I need help (Chain Projectile)
Hello. can not speak English well. First of all, I'm so sorry about very poor English of mine.
I would like someone to tell me how to make a chain projectile The way i've done it so far is something like this I thought about adding enemy information on the list, using OnTriggerEnter2D, OverlapCircleAll now the problem is how to connect the projectile to the enemy and damage it but I don't get the idea. Could you please give me some help? I'd be grateful of any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChainProjectile : MonoBehaviour
{
[SerializeField] private float moveSpeed = 10f;
[SerializeField] private float minDistanceToDealDamage = 0.1f;
public ChainTowerProjectile ChainTowerOwner { get; set; }
public float Damage { get; set; }
private int targetIndex;
private Enemy _enemyTarget;
private List<Transform> _enemies = new List<Transform>();
private void MoveChainProjectile()
{
transform.position = Vector2.MoveTowards(transform.position,
_enemyTarget.transform.position, moveSpeed * Time.deltaTime);
float distanceToTarget =
(_enemyTarget.transform.position - transform.position).magnitude;
if (distanceToTarget < minDistanceToDealDamage)
{
_enemyTarget.EnemyHealth.DealDamage(Damage);
ChainTowerOwner.ResetTowerProjectile();
ObjectPooler.ReturnToPool(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Enemy"))
{
Collider2D[] enemyInfo = Physics2D.OverlapCircleAll(transform.position, 0f);
foreach (Collider2D chainEnemy in enemyInfo)
{
if (chainEnemy.CompareTag("Enemy"))
{
_enemies.Add(chainEnemy.transform);
}
}
}
}
public void SetEnemy(Enemy enemy)
{
_enemyTarget = enemy;
}
public void ResetChainProjectile()
{
_enemyTarget = null;
}
}`public class ChainTowerProjectile : MonoBehaviour
{ [SerializeField] private Transform projectileSpawnPosition; [SerializeField] private float delayBtwAttacks = 0f; [SerializeField] private float damage = 0f;
private float _nextAttackTime;
private ObjectPooler _pooler;
private Tower _tower;
private ChainProjectile _chainProjectileLoaded;
private void Start()
{
_tower = GetComponent<Tower>();
_pooler = GetComponent<ObjectPooler>();
}
private void LoadProjectile()
{
GameObject newInstance = _pooler.GetInstanceFromPool();
newInstance.transform.localPosition = projectileSpawnPosition.position;
newInstance.transform.SetParent(projectileSpawnPosition);
_chainProjectileLoaded = newInstance.GetComponent<ChainProjectile>();
_chainProjectileLoaded.ChainTowerOwner = this;
_chainProjectileLoaded.ResetChainProjectile();
newInstance.SetActive(true);
}
private bool IsTowerEmpty()
{
return _chainProjectileLoaded == null;
}
public void ResetTowerProjectile()
{
_chainProjectileLoaded = null;
}
} `
Follow this Question
Related Questions
c# cods spaceship 0 Answers
How to Correct facelight.enabled coding error in Survival Shooter 0 Answers
Need Help With Arduino Code to Unity 0 Answers
How does this code work? 0 Answers