BallShot.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BallShot : MonoBehaviour { public GameObject prefab; //ボールのPrefabデータを入れる public float power; //ボールに加える力 private float time; //連射防止 void Start() { time = 2.0f; //タイムを2秒で始めて、最初の1回は発射OK } void Update() { time += Time.deltaTime; //連射防止 カウントアップタイマー if(time >= 2.0f) //連射防止 2秒たてば実行 { if(Input.GetMouseButtonDown(0)) //マウス左クリックしたとき { //Prefabのボールを生成 GameObject ball = GameObject.Instantiate(prefab) as GameObject; //ボールの発射位置をControllerの位置にする ball.transform.position = this.transform.position; //マウスクリックした位置にRayを飛ばす Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //rayの方向を取得し、targetに格納 Vector3 target = ray.direction; //クリックしたところに向けて、ボールに加速度をつける ball.GetComponent<Rigidbody>().velocity = target * power; time = 0.0f; //連射防止 タイマーを0に戻す } } } } |