Hello there, I have lil problem Im still learning coding C# and I can’t figure out how to stop my enemy from killing the player when it enters on Collision with it when the Player has the shield on, any thoughs on that I 'll really apreciate it.
here’s my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody theRB;
public ParticleSystem explosionParticle;
public bool hasShield = false;
public bool gameOver;
private Animator animator;
public GameObject ShieldIndicator;
// Start is called before the first frame update
void Start()
{
theRB = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
}
private void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag ("PowerPill"))
{
hasShield = true;
ShieldIndicator.gameObject.SetActive(true);
gameObject.GetComponent<Animator>().Play("Xplosion");
StartCoroutine(ShieldCountDownRoutine());
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy") && hasShield)
{
Rigidbody enemyRigidbody = collision.gameObject.GetComponent<Rigidbody>();
}
if (collision.gameObject.CompareTag("Enemy"))
{
Debug.Log("Game Over");
gameOver = true;
Destroy(gameObject);
}
}
IEnumerator ShieldCountDownRoutine()
{
yield return new WaitForSeconds(5);
hasShield = false;
ShieldIndicator.gameObject.SetActive(false);
}
}