Created
August 29, 2019 06:57
-
-
Save IshidaGames/1616183021ca120b42204c225fb9a93e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Blast : MonoBehaviour | |
{ | |
//爆破する力 | |
public float power = 1000f; | |
//爆破する範囲 | |
public float radius = 3.0f; | |
Rigidbody rb; | |
Ray ray; | |
RaycastHit hit; | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
//クリックした場所にカメラからの仮想の線、Rayを飛ばす | |
ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
//Rayが当たったら作動 | |
if (Physics.Raycast(ray, out hit, 100f)) | |
{ | |
//クリックした所から半径3mの範囲のColliderを取得 | |
Collider[] cols = Physics.OverlapSphere(hit.point, 3f); | |
//取得したColliderすべてに作動 | |
foreach (Collider cube in cols) | |
{ | |
//範囲内のオブジェクトのRigidbodyに爆破の力を作用させる | |
if (cube.GetComponent<Rigidbody>()) | |
{ | |
cube.GetComponent<Rigidbody>(). | |
AddExplosionForce(power, hit.point, radius); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment