Files
GMTKGameJam2020/Assets/Scripts/EnemyScript.cs
T

59 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
2020-07-12 01:34:47 -04:00
public GameObject particleEffect;
2020-07-12 01:07:52 -04:00
public AudioClip[] spawnSounds;
public AudioClip[] defeatSounds;
2020-07-11 11:23:39 -04:00
private float speed;
private GameObject player;
2020-07-12 01:07:52 -04:00
private AudioSource audioSource;
void Start()
{
2020-07-12 01:07:52 -04:00
audioSource = GetComponent<AudioSource>();
player = GameObject.FindWithTag("Player");
2020-07-11 11:23:39 -04:00
speed = Random.Range(0.5f, 3.0f);
2020-07-12 01:07:52 -04:00
audioSource.clip = spawnSounds[Random.Range(0,spawnSounds.Length)];
audioSource.Play();
}
// Update is called once per frame
void Update()
{
if(player != null)
{
transform.position = Vector2.MoveTowards(
transform.position, player.transform.position, speed * Time.deltaTime);
}
}
void OnCollisionEnter2D(Collision2D other)
{
2020-07-11 16:19:57 -04:00
if(other.gameObject.tag == "Bullet")
{
PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score")+1);
2020-07-12 01:07:52 -04:00
AudioSource.PlayClipAtPoint(
defeatSounds[Random.Range(0,defeatSounds.Length)], new Vector3(0, 0, 0));
2020-07-12 01:34:47 -04:00
Instantiate(particleEffect, transform.position, transform.rotation);
2020-07-11 16:19:57 -04:00
Destroy(gameObject);
}
if(other.gameObject.tag == "Player")
{
2020-07-12 01:07:52 -04:00
AudioSource.PlayClipAtPoint(
defeatSounds[Random.Range(0,defeatSounds.Length)], new Vector3(0, 0, 0));
2020-07-12 01:34:47 -04:00
Instantiate(particleEffect, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}