horiaci_drak/Assets/scripts/enemykiller.cs
2022-04-27 20:35:00 +02:00

67 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//include textmeshpro
using TMPro;
public class enemykiller : MonoBehaviour
{
// Start is called before the first frame update
//get textmeshpro text
public TextMeshProUGUI text;
public ParticleSystem part;
public List<ParticleCollisionEvent> collisionEvents;
public int score = 0;
void Start()
{
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
}
// Update is called once per frame
void Update()
{
}
//on particle coolision
void OnParticleCollision(GameObject otherx)
{
int numCollisionEvents = part.GetCollisionEvents(otherx, collisionEvents);
int i = 0;
while (i < numCollisionEvents)
{
GameObject other = collisionEvents[i].colliderComponent.gameObject;
if (other.tag == "enemy")
{
Destroy(other);
Debug.Log("enemy killed");
//spawn new enemy
GameObject enemyclone = Instantiate(other, new Vector3(Random.Range(10, 100), Random.Range(-70, -60), Random.Range(-90, 0)), Quaternion.identity);
//set enemy name
enemyclone.name = "enemy" + i;
enemyclone.tag = "enemy";
//increase score
score++;
//update text
text.text = "Score: " + score;
}
if (other.tag == "king")
{
Destroy(other);
Debug.Log("killed king");
//spawn new king
GameObject kingclone = Instantiate(other, new Vector3(Random.Range(10, 100), Random.Range(-70, -60), Random.Range(-90, -20)), Quaternion.identity);
//set king name
kingclone.name = "king" + i;
kingclone.tag = "king";
//decrease score
score-= 10;
//update text
text.text = "Score: " + score;
}
i++;
}
}
}