Hello,
This is in 2D. I have my player, an enemy, and a projectile from the enemy. The projectile is destroyed when it hits the player. I put a bool on the projectile that when it hits the player, it turns true. I have this coded in Update on the enemy that when the bool on the projectile turns true, and animation goes off for the enemy. In the console, when the projectile hits the player, the bool is true. However, it is not true on enemy script. Any ideas why?
Here is the script on the projectile.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileHitPlayer : MonoBehaviour
{
public bool projectileHit;
public GameObject enemy;
void Start()
{
projectileHit = false;
}
void OnTriggerEnter2D(Collider2D colproj)
{
if (colproj.gameObject.CompareTag("Player"))
{
projectileHit = true;
Destroy(gameObject);
}
}
}
Here is the script on the Enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHitPlayer : MonoBehaviour
{
Animator anim;
bool hit;
public GameObject enemyProj;
ProjectileHitPlayer projectileHitPlayer;
void Start()
{
hit = false;
anim = GetComponent<Animator>();
projectileHitPlayer = enemyProj.GetComponent<ProjectileHitPlayer>();
}
void Update()
{
if (!hit)
{
if (projectileHitPlayer.projectileHit == true)
{
hit = true;
anim.SetTrigger("HitPlayer");
}
}
}
}
Anyone help on this is great.
Also one last question: I have three boxes and a script on box 1. I want the script to say that when box 2 and box 3 collide, box 1 does something. Is that possible to do and if so how?
Thanks!
Nic