やりたいこと
タイトル通り、unityのtilemapを使用して、戦略シミュレーションのように、ある駒をマウスカーソルでクリックすることでクレーンゲームのようにつかみ、もう一度押すことで駒をもとの位置から動かしたい。
現状の問題点
駒がtileではなく、Transformに従って動くため、tileの上に駒を配置することが出来ない。(画像参照)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Tilemaps; 5 6public class Test_player_tile : MonoBehaviour 7{ 8 [SerializeField] 9 private Tilemap map; 10 public bool mouse_onoff; 11 public Vector3Int position; 12 [SerializeField] 13 private Game_Manager manager; 14 [SerializeField] 15 private Test_tilemap tile; 16 // Start is called before the first frame update 17 void Start() 18 { 19 mouse_onoff = false; 20 position = map.WorldToCell(this.gameObject.transform.position); 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 var a = this.transform.position; 27 a.z = -1; 28 this.transform.position = tile.clickposition; 29 //position = map.WorldToCell(this.gameObject.transform.position); 30 if (Input.GetMouseButtonDown(0)) 31 { 32 Debug.Log(position); 33 this.gameObject.transform.position = position; 34 } 35 } 36} 37
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Game_Manager : MonoBehaviour 6{ 7 private Vector3 position; 8 private Vector3 screenToWorldPointPosition; 9 [SerializeField] 10 private Test_tilemap testmap; 11 [SerializeField] 12 private GameObject player; 13 [SerializeField] 14 private Test_player_tile player_script; 15 public Vector3Int mouse_position; 16 private bool key; 17 // Start is called before the first frame update 18 void Start() 19 { 20 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 position = Input.mousePosition; 27 position.z = 10f; 28 screenToWorldPointPosition = Camera.main.ScreenToWorldPoint(position); 29 mouse_position = testmap.ClickedAction(screenToWorldPointPosition); 30 if (Input.GetMouseButtonDown(0)) 31 { 32 Debug.Log(this.mouse_position); 33 if (testmap.clickposition == player_script.position) 34 { 35 player_script.mouse_onoff = true; 36 key = player_script.mouse_onoff; 37 Debug.Log("OK!"); 38 } 39 } 40 } 41} 42
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Tilemaps; 5 6public class Test_tilemap : MonoBehaviour 7{ 8 public TileBase testtile; 9 public Tilemap a; 10 public Vector3Int clickposition; 11 // Start is called before the first frame update 12 void Start() 13 { 14 15 } 16 public Vector3Int ClickedAction(Vector3 position) 17 { 18 clickposition = a.WorldToCell(position); 19 return clickposition; 20 } 21 // Update is called once per frame 22} 23
どうかよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。