Hello everyone,currently i’m making a projectile type that bounce between enemy like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BounceBullet : MonoBehaviour {
public float bounceTime = 0, speed;
private Vector2 movedirection,direction;
private Rigidbody2D mybody;
private CircleCollider2D cirCol;
private GameObject target;
private List monsters = new List();
private void Awake()
{
mybody = GetComponent();
cirCol = GetComponent();
}
void FixedUpdate()
{
{
if (target == null)
{
cirCol.radius += 4f;
}
else
{
bounceTime = 0f;
cirCol.radius = 0.2f;
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}
}
bounceTime += Time.deltaTime;
if (bounceTime > 0.2f)
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "enemy")
{
if (monsters.Contains(other.gameObject))
{
target = null;
}
else
{
monsters.Add(other.gameObject);
target = other.gameObject;
}
}
}
}
The projectile has a small circle collider(0.2f radius).
The projectiles kinda bouncy but not really like what i want, the way they bounce is kinda weird, they dont bounce exactly like the video. And there is one big issue: when my character stay very near enemy and shoot, the bullet just stick to the enemy’s center and not moving else where, they are not destroyed. But when that enemy die, these bullets start to bounce to other enemy…