- Home /
How to make projectiles bounce between enemy?
Hello everyone,currently i'm making a projectile type that bounce between enemy like this : https://www.youtube.com/watch?v=7TCPl446TcI
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<GameObject> monsters = new List<GameObject>();
private void Awake()
{
mybody = GetComponent<Rigidbody2D>();
cirCol = GetComponent<CircleCollider2D>();
}
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..
Answer by tormentoarmagedoom · Sep 28, 2018 at 01:31 PM
Good day.
Use this to calculate the new angle
https://docs.unity3d.com/ScriptReference/Vector3.Reflect.html
And change bullet direction to new vector.
bye!