転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【Unity】マウスをクリックしたら、弾を発射する方法

public GameObject bullet;
	
// Update is called once per frame
void Update () {

    if(Input.GetMouseButtonDown(0))
    {

        shot();

    }
        
}

void shot()
{

    Instantiate(bullet, transform.position, transform.rotation);

}

ちなみに、弾の方のスクリプトは以下。

 public int speed = 10;

Rigidbody rb;

// Use this for initialization
void Start () {

    rb = GetComponent<Rigidbody>();
    rb.velocity = transform.up.normalized * speed;

}