using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody2D rigidbody;
public Text collectedTxt;
public static int amount;
// Start is called before the first frame update
void Start()
{
this.rigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
this.rigidbody.velocity = new Vector3(horizontal * speed, vertical * speed, 0);
this.collectedTxt.text = "Items Collected: " + amount;
}
}
using UnityEngine;
public class Item : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
PlayerController.amount++;
Debug.Log("Get heart");
Destroy(this.gameObject);
}
}
}
using UnityEngine;
public class heart : MonoBehaviour
{
public AudioSource sound;
private void Start()
{
this.sound = GetComponent<AudioSource>();
}
public void Init()
{
this.sound.Play();
}
}
using UnityEngine;
public class Item : MonoBehaviour
{
public heart heart;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
PlayerController.amount++;
Debug.Log("Get heart");
this.heart.Init();
Destroy(this.gameObject);
}
}
}
이동과 눈물 발사 키 따로 구현
using UnityEngine;
public class BulletController : MonoBehaviour
{
public float lifeTime;
public AudioSource sound;
// Start is called before the first frame update
void Start()
{
StartCoroutine(this.DestroyDelay());
this.sound.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
private IEnumerator DestroyDelay()
{
this.playSound();
yield return new WaitForSeconds(lifeTime);
Destroy(this.gameObject);
}
private void playSound()
{
this.sound.Play();
}
}
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody2D rigidbody;
public Text collectedTxt;
public static int amount;
public GameObject bulletPrefab;
public float bulletSpeed;
private float lastFire;
public float fireDelay;
// Start is called before the first frame update
void Start()
{
this.rigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float shootHor = Input.GetAxis("ShootHorizontal");
float shootVert = Input.GetAxis("ShootVertical");
if ((shootHor != 0 || shootVert != 0) && Time.time > lastFire + fireDelay)
{
this.Shoot(shootHor, shootVert);
lastFire = Time.time;
}
this.rigidbody.velocity = new Vector3(horizontal * speed, vertical * speed, 0);
this.collectedTxt.text = "Items Collected: " + amount;
}
void Shoot(float x, float y)
{
GameObject bullet = Instantiate(this.bulletPrefab, transform.position, transform.rotation) as GameObject;
bullet.AddComponent<Rigidbody2D>().gravityScale = 0;
bullet.GetComponent<Rigidbody2D>().velocity = new Vector3(
(x < 0) ? Mathf.Floor(x) * bulletSpeed : Mathf.Ceil(x) * bulletSpeed,
(y < 0) ? Mathf.Floor(y) * bulletSpeed : Mathf.Ceil(y) * bulletSpeed,
0);
}
}
맵 벽과 몬스터까지 구현했는데... 몬스터 상태 왜 저러냐 ㅅㅂ
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public enum eEnemyState
{
WANDER, FOLLOW, DIE
};
GameObject player;
public eEnemyState currState = eEnemyState.FOLLOW;
public float range;
public float speed;
private bool chooseDir = false;
private bool isDead = false;
private Vector3 randDir;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
switch (currState)
{
case (eEnemyState.WANDER):
{
this.Wander();
break;
}
case (eEnemyState.FOLLOW):
{
this.Follow();
break;
}
case (eEnemyState.DIE):
{
this.Die();
break;
}
}
if (isPlayerInRange(range) && currState != eEnemyState.DIE)
{
currState = eEnemyState.FOLLOW;
}
else if (!isPlayerInRange(range) && currState != eEnemyState.DIE)
{
currState = eEnemyState.WANDER;
}
}
private bool isPlayerInRange(float range)
{
return Vector3.Distance(transform.position, player.transform.position) <= this.range;
}
private void Wander()
{
if (!this.chooseDir)
{
StartCoroutine(ChooseDir());
}
transform.position += -transform.right * speed * Time.deltaTime;
if (isPlayerInRange(range))
{
currState = eEnemyState.FOLLOW;
}
}
private IEnumerator ChooseDir()
{
this.chooseDir = true;
yield return new WaitForSeconds(Random.Range(2f, 8f));
this.randDir = new Vector3(0, 0, Random.Range(0, 360));
Quaternion nextRotation = Quaternion.Euler(randDir);
this.transform.rotation = Quaternion.Lerp(transform.rotation, nextRotation, Random.Range(0.5f, 2.5f));
chooseDir = false;
}
private void Follow()
{
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
}
private void Die()
{
Destroy(this.gameObject, 0);
}
}