Skip to content

Instantly share code, notes, and snippets.

@aidiary
Last active October 11, 2018 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidiary/b9600d04c6654eaa8c07 to your computer and use it in GitHub Desktop.
Save aidiary/b9600d04c6654eaa8c07 to your computer and use it in GitHub Desktop.
大砲から砲弾を発射する
#pragma strict
// 砲弾に加える力
var power : float = 30000;
// 砲弾オブジェクト(インスペクタからセット)
var shellObject : GameObject;
// 発射音(インスペクタからセット)
var bomb : AudioClip;
function Update () {
if (Input.GetMouseButtonDown(0)) {
// メインカメラから見たマウスの方向を取得
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var dir : Vector3 = ray.direction.normalized;
// 砲弾を作成
var shell = Instantiate(shellObject, transform.position, transform.rotation);
// マウスの方向へ力を加えて発射
shell.rigidbody.AddForce(dir * power, ForceMode.Impulse);
// 発射音を再生
audio.PlayOneShot(bomb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment