Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active November 28, 2020 23:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Buravo46/8018394 to your computer and use it in GitHub Desktop.
Save Buravo46/8018394 to your computer and use it in GitHub Desktop.
【Unity】クリックした位置にPrefabを生成するスクリプト。
using UnityEngine;
using System.Collections;
public class ClickPositionCreatePrefabScript : MonoBehaviour {
// 生成したいPrefab
public GameObject Prefab;
// クリックした位置座標
private Vector3 clickPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// マウス入力で左クリックをした瞬間
if (Input.GetMouseButtonDown(0)) {
// ここでの注意点は座標の引数にVector2を渡すのではなく、Vector3を渡すことである。
// Vector3でマウスがクリックした位置座標を取得する
clickPosition = Input.mousePosition;
// Z軸修正
clickPosition.z = 10f;
// オブジェクト生成 : オブジェクト(GameObject), 位置(Vector3), 角度(Quaternion)
// ScreenToWorldPoint(位置(Vector3)):スクリーン座標をワールド座標に変換する
Instantiate(Prefab, Camera.main.ScreenToWorldPoint(clickPosition), Prefab.transform.rotation);
}
}
}
#pragma strict
// 生成したいPrefab
var Prefab : GameObject;
// クリックした位置座標
private var clickPosition : Vector3;
function Start () {
}
function Update () {
// マウス入力で左クリックをした瞬間
if (Input.GetMouseButtonDown(0)) {
// ここでの注意点は、例えPrefabがSpriteだからといって座標の引数にVector2を渡すのではなく、Vector3を渡すことである。
// Vector3でマウスがクリックした位置座標を取得する
clickPosition = Input.mousePosition;
// Z軸修正
clickPosition.z = 10f;
// オブジェクト生成 : オブジェクト(GameObject), 位置(Vector3), 角度(Quaternion)
// ScreenToWorldPoint(位置(Vector3)):スクリーン座標をワールド座標に変換する
Instantiate(Prefab, Camera.main.ScreenToWorldPoint(clickPosition), Prefab.transform.rotation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment