Last active
March 27, 2024 06:44
-
-
Save Buravo46/8367810 to your computer and use it in GitHub Desktop.
【Unity】マウスの座標とGameObjectの座標を同期するスクリプト。
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 UnityEngine; | |
using System.Collections; | |
public class MouseSynchronizeObjectScript : MonoBehaviour { | |
// 位置座標 | |
private Vector3 position; | |
// スクリーン座標をワールド座標に変換した位置座標 | |
private Vector3 screenToWorldPointPosition; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
// Vector3でマウス位置座標を取得する | |
position = Input.mousePosition; | |
// Z軸修正 | |
position.z = 10f; | |
// マウス位置座標をスクリーン座標からワールド座標に変換する | |
screenToWorldPointPosition = Camera.main.ScreenToWorldPoint(position); | |
// ワールド座標に変換されたマウス座標を代入 | |
gameObject.transform.position = screenToWorldPointPosition; | |
} | |
} |
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
#pragma strict | |
// 位置座標 | |
private var position : Vector3; | |
// スクリーン座標をワールド座標に変換した位置座標 | |
private var screenToWorldPointPosition : Vector3; | |
function Start () { | |
} | |
function Update () { | |
// Vector3でマウス位置座標を取得する | |
position = Input.mousePosition; | |
// Z軸修正 | |
position.z = 10f; | |
// マウス位置座標をスクリーン座標からワールド座標に変換する | |
screenToWorldPointPosition = Camera.main.ScreenToWorldPoint(position); | |
// ワールド座標に変換されたマウス座標を代入 | |
gameObject.transform.position = screenToWorldPointPosition; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment