유니티 심화

유니티 슈팅게임

송현호 2023. 8. 16. 16:50
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    private Transform tr;
    public float moveSpeed = 10.0f;
    private Animator anim;
    [SerializeField]
    private Bullet bullet;
    // Start is called before the first frame update
    void Start()
    {
        tr = GetComponent<Transform>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Bullet bl = Instantiate(bullet);
            bl.transform.position = this.transform.position;
        }
    }

    private void Move()
    {
        //카메라뷰로 이동제한
        Vector3 worldPos = Camera.main.WorldToViewportPoint(this.transform.position);
        if (worldPos.x < 0.1f) worldPos.x = 0.1f;
        if (worldPos.x > 0.9f) worldPos.x = 0.9f;
        if (worldPos.y < 0.08f) worldPos.y = 0.08f;
        if (worldPos.y > 0.92f) worldPos.y = 0.92f;
        this.transform.position = Camera.main.ViewportToWorldPoint(worldPos);

        //이동
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector2 moveDir = new Vector2(h, v);
        Vector2 dir = moveDir.normalized;
        tr.Translate(dir * moveSpeed * Time.deltaTime);

        if (h > 0)
        {
            this.anim.SetInteger("State", 2);
        }
        else if (h < 0)
        {
            this.anim.SetInteger("State", 1);
        }
        else
        {
            this.anim.SetInteger("State", 0);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private float moveSpeed = 20.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(Vector2.up * moveSpeed * Time.deltaTime);
        Vector3 worldPos = Camera.main.WorldToViewportPoint(this.transform.position);
        if (worldPos.y > 1f)
        {
            Destroy(this.gameObject);
        }
    }
}