,I'm having trouble rebounding multiple projectiles with the same tag off a game object.
Hi, So, I have a projectile that will constantly move right until it hits a game object with a box collider. Once hit, the projectile will then move left. My problem is that when I duplicate the object and and run the game, only the original projectile will rebound and the duplicated will not. Sometimes, when the original projectile hits the game object, the duplicated one starts moving left instead of the original. This is the code for my projectile.
public class PinkProjectile : MonoBehaviour
{
private Transform player;
private MouthCollision totalPoints;
public float speed = 100f;
float reverseSpeed = 150f;
public Rigidbody2D pinkRigid;
public GameObject pinkObject;
public bool bodyHit;
// Start is called before the first frame update
void Start()
{
totalPoints = GameObject.FindGameObjectWithTag("Mouth").GetComponent<MouthCollision>();
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
void FixedUpdate()
{
//move the projectile towards the player, if the distance between the projectile and player is > 0, then execute if statement
if (Vector2.Distance(pinkRigid.transform.position, player.position) > 0 && bodyHit == false)
{
moveProjectileRight();
}
if (Vector2.Distance(pinkRigid.transform.position, player.position) > 0 && bodyHit == true)
{
moveProjectileLeft();
}
}
void moveProjectileRight()
{
if (totalPoints.totalPoints >= 5)
{
speed += 0.2f;
}
else if (totalPoints.totalPoints >= 10)
{
speed += 0.3f;
}
else if (totalPoints.totalPoints >= 15)
{
speed = 0.5f;
}
else if (totalPoints.totalPoints >= 20)
{
speed = 1f;
}
pinkRigid.velocity = transform.right * speed * Time.deltaTime;
}
void moveProjectileLeft()
{
//moves the pink projectile left as the transform is minus
pinkRigid.velocity = -transform.right * reverseSpeed * Time.deltaTime;
}
}
Here is the snippet of code in the game object that the projectile collides with:
if (other.gameObject.tag == "PinkProjectile")
{
pinkAnim.SetTrigger("pinkHitLeft");
pink.bodyHit = true;
}
I think I know the problem , and it is to do with the boolean "bodyHit" variable where it is connected to all the duplicated projectiles so once that turns true, it is true for all the other projectiles as they have the same tag. If that is the problem is there another way to go about this?? Or will I have to create separate scripts for each projectile that I want to add (I have tried this and it works but it is VERY time consuming and messy).
Any help would be much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011529im_/https://answers.unity.com/themes/thub/images/avi.jpg)