UnityでC#を用いてボードゲームを作っています。
コマの移動を行いたいのですがうまくいきません。とりあえずコマを動かしたいのでクリックしたら指定の座標に移動(瞬間移動)するようにしたいです。transform.positionやtransform.Translateを使用しても動きませんでした。
ゲームの流れを示すGameMasterクラスです。Update()でクリックしたら指定したオブジェクトのメソッドを呼び出すようにしています。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 private int turn = 0; 5 private GameObject gameboard; 6 private Piece[] pl = new Piece[2]; 7 private GameObject Piece1; 8 private Piece piece1; 9 private GameObject Piece2; 10 private Piece piece2; 11 12 void Start(){ 13 //盤の作成 14 gameboard = GameObject.Find("GameBoard"); 15 GameBoard board = gameboard.GetComponent<GameBoard>(); 16 //コマの配置 17 Piece1 = (GameObject)Resources.Load ("prefabs/Piece"); 18 piece1 = Piece1.GetComponent<Piece>(); 19 Instantiate(Piece1,new Vector3(3,1,0),Quaternion.identity); 20 piece1.FirstSet(3,0,0); 21 pl[0] = piece1; 22 23 Piece2 = (GameObject)Resources.Load ("prefabs/Piece2"); 24 piece2 = Piece2.GetComponent<Piece>(); 25 Instantiate(Piece2,new Vector3(2,1,5),Quaternion.identity); 26 piece2.FirstSet(2,5,1); 27 pl[1] = piece2; 28 } 29 30 void Update(){ 31 if(Input.GetMouseButtonDown(0)){ 32 pl[turn].move(3,2); 33 } 34 }
コマを示すPieceクラスです。move()で引数の位置に自身を動かします。start(),Update()内には何も記述していません。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 private int posx; 5 private int posy; 6 private int posz; 7 private int own; 8 public void FirstSet(int x,int z,int turn){ 9 posx = x; 10 posy = 1; 11 posz = z; 12 own = turn; 13 } 14 15 public void move(int x,int z){ 16 this.transform.position = new Vector3(x,posy,z); 17 }
以下はゲーム盤のクラスです。おそらく本質問には関係ないと思われますが念のため載せておきます。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GameBoard : MonoBehaviour 6{ 7 const int BOARDSIZE = 6; 8 private GameObject[,] cell = new GameObject[BOARDSIZE,BOARDSIZE]; 9 void Start(){ 10 for(int i = 0; i < 6; i++){ 11 for(int j = 0; j < 6; j++){ 12 if((i + j) % 2 == 0){ 13 GameObject cell1 = (GameObject)Resources.Load ("prefabs/cell1"); 14 Instantiate(cell1,new Vector3(i,0,j),Quaternion.identity); 15 cell[i,j] = cell1; 16 }else{ 17 GameObject cell2 = (GameObject)Resources.Load ("prefabs/cell2"); 18 Instantiate(cell2,new Vector3(i,0,j),Quaternion.identity); 19 cell[i,j] = cell2; 20 } 21 } 22 } 23 } 24} 25
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。