using System; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { public float speed = 3; public GameObject bulletPrefab; Vector3 move; public Material flashMaterial; public Material defaultMaterial; public AudioClip shotSound; public AudioClip hitSound; public AudioClip deadSound; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void Update() { move = Vector3.zero; if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { //transform.Translate(new Vector3( -speed * Time.deltaTime, 0, 0)); move += new Vector3(-1, 0, 0); } if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) { move += new Vector3(0, 1, 0); } if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { move += new Vector3(1, 0, 0); } if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) { move += new Vector3(0, -1, 0); } move = move.normalized; if (move.x < 0) { GetComponent().flipX = true; } if (move.x > 0) { GetComponent().flipX = false; } if (move.magnitude > 0) { GetComponent().SetTrigger("Move"); } else { GetComponent().SetTrigger("Stop"); } if (Input.GetMouseButtonDown(0)) { Shoot(); } } void Shoot() { GetComponent().PlayOneShot(shotSound); Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // Debug.Log(worldPosition); worldPosition.z = 0; worldPosition -= (transform.position + new Vector3(0, -0.5f, 0)); //GameObject newBullet = Instantiate(bulletPrefab); GameObject newBullet = GetComponent().Get(); if (newBullet != null) { // ??? ??? ???? ??? ??? ???? newBullet.transform.position = transform.position + new Vector3(0, -0.5f); //newBullet.GetComponent().Direction = new Vector2(1, 0); newBullet.GetComponent().Direction = worldPosition; } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Enemy") { if (GetComponent().Hit(1)) { // »ì¾ÆÀÖ´Ù. GetComponent().PlayOneShot(hitSound); Flash(); } else { // Á×Àº »óÅ Die(); GetComponent().PlayOneShot(deadSound); } } } private void FixedUpdate() { transform.Translate(move * speed * Time.fixedDeltaTime); } void Flash() { GetComponent().material = flashMaterial; Invoke("AfterFlash", 0.5f); } void AfterFlash() { GetComponent().material = defaultMaterial; } void Die() { GetComponent().SetTrigger("Die"); // ÀÏÁ¤ ½Ã°£ÀÌ Áö³­ ÈÄ È£ÃâÇÏ´Â ÇÔ¼ö Invoke("AfterDying", 0.875f); } void AfterDying() { // Destroy(gameObject); // gameObject.SetActive(false); SceneManager.LoadScene("GameOverScene"); } }