エッセイマンガ!オタクビアンカップル同棲日記「よめよめ」

同棲中のレズビアンカップルのゆんとあゆむが、日々の生活の様子をマンガや日記にしてお届けするブログです

Unity2Dゲーム制作!簡単にできる画像の左右反転!

 

 

BulletManager

    private GameObject playerControl;
    private float SPEED = 0.03f;
    private int direction = 1; // 方向


    // Start is called before the first frame update
    void Start()
    {
        playerControl = GameObject.Find("Idle (32x32)_0");
        Destroy(gameObject, 0.3f);
    }

    // Update is called once per frame
    void Update()
    {
        direction = playerControl.GetComponent<PlayerControl>().direction;
        Vector2 position = transform.position;

        position.x += SPEED * direction;
       
        transform.position = position;
    }

 

 

 

PlayerControl

    private float SPEED = 0.01f;    // 移動速度
    private float JUMP = 2.0f;    // ジャンプの高さ
    private int ground = 0;    // 接地判定
    public int direction = 1; // 方向

    [SerializeField] GameObject gameOverText;
    [SerializeField] GameObject bullet;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // キャラクター操作
        Vector2 position = transform.position;

        if (Input.GetKey(KeyCode.RightArrow))
        {            
            position.x += SPEED;
            direction = 1;
            transform.localScale = new Vector2(4, 4);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            position.x -= SPEED;
            direction = -1;
            transform.localScale = new Vector2(-4, 4);
        }
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            if (ground == 0)
            {
                Debug.Log("Jump");
                position.y += JUMP;
            }
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(bullet, transform.position, transform.rotation);
            Debug.Log("shot");
        }

        transform.position = position;
    }

    // トラップ接触判定
    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "Trap")
        {
            GameOver(); 
        }
    }

    // 接地判定
    void OnTriggerStay2D(Collider2D col)
    {
        if (col.tag == "Ground")
        {
            ground = 0;
        }
    }
    // ジャンプ判定
    void OnTriggerExit2D(Collider2D col)
    {
        if (col.tag == "Ground")
        {
            ground = 1;
        }
    }

    // GameOver処理
    void GameOver()
    {
        gameOverText.SetActive(true);
        this.gameObject.SetActive(false);
        Debug.Log("GameOver");
    }