0% found this document useful (0 votes)
27 views1 page

Cannon Robot Script

This C# script controls enemy behavior in a 2D game. It finds the player's cannon controller on start. When the enemy collides with a cannonball, it instantiates a fire explosion at the enemy's position, disables the enemy's sprite renderer and box collider to remove it from the scene.

Uploaded by

Cristi Popa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Cannon Robot Script

This C# script controls enemy behavior in a 2D game. It finds the player's cannon controller on start. When the enemy collides with a cannonball, it instantiates a fire explosion at the enemy's position, disables the enemy's sprite renderer and box collider to remove it from the scene.

Uploaded by

Cristi Popa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

public GameObject fireExplosion;

private CannonController cannonController;


private GameObject cannonChecker;

private void Start()


{
cannonChecker = GameObject.FindGameObjectWithTag("Player");
if (cannonChecker != null)
{
cannonController = cannonChecker.GetComponent<CannonController>();
}
else Debug.Log("Cannont find cannon controller");
GetComponent<SpriteRenderer>().enabled = true;
GetComponent<BoxCollider2D>().enabled = true;
}

private void OnTriggerEnter2D(Collider2D collision)


{
if(collision.tag == "CannonBall")
{
Instantiate(fireExplosion, transform.position, transform.rotation);
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<BoxCollider2D>().enabled = false;
}
}
}

You might also like