ランダムなTransformを取得して、targetに入れたいのですが、方法が分かりません。
試した方法は、
C#
1public GameObject obj; 2public Transform target; 3protected override void Start() 4{ 5 int x = Random.Range(4,104); 6 int y = Random.Range(4,104); 7 obj.transform.position = new Vecotor3(x,y,0); 8 target = obj.transform; 9}
をやってみましたが、多少は動くのですが、現在位置と隣のマスを行き来するだけで、きちんと動きませんでした。
C#
1public Transform target; 2protected override void Start() 3{ 4 target = GameObject.FindGameObjectWithTag ("Food").transform; 5}
もやってみました。ですが、30体生成しているオブジェクトのほとんどが、Foodオブジェクトを百個以上生成しているのに、みな、同一のFoodオブジェクトの所に向かってしまい、動きにランダム性が見られなくなってしまいました。
もしランダムなTransformを取得する方法があれば、教えて欲しいです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class Enemy : MovingObject 7{ 8 public Transform target; 9 private Statistics empty; 10 11 private Statistics statistics; 12 13 public Vector3 enemypositoon; 14 15 // Use this for initialization 16 protected override void Start() 17 { 18 GameManager.instance.AddEnemyToList(this); 19 20 base.Start(); 21 22 empty = GetComponent<Statistics>(); 23 } 24 25 private void Update() 26 { 27 if (empty.HP <= 0) 28 { 29 GameManager.instance.RemoveEnemyToList(this); 30 Destroy(gameObject); 31 } 32 33 enemypositoon = transform.position; 34 } 35 36 protected override void AttemptMove<T>(int xDir, int yDir) 37 { 38 empty = GetComponent<Statistics>(); 39 empty.HP--; 40 //Call the AttemptMove function from MovingObject. 41 base.AttemptMove<T>(xDir, yDir); 42 } 43 44 //MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player. 45 public void MoveEnemy() 46 { 47 //Declare variables for X and Y axis move directions, these range from -1 to 1. 48 //These values allow us to choose between the cardinal directions: up, down, left and right. 49 int xDir = 0; 50 int yDir = 0; 51 52 //If the difference in positions is approximately zero (Epsilon) do the following: 53 if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon) 54 55 //If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down). 56 yDir = target.position.y > transform.position.y ? 1 : -1; 57 58 59 //If the difference in positions is not approximately zero (Epsilon) do the following: 60 else 61 //Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left). 62 xDir = target.position.x > transform.position.x ? 1 : -1; 63 64 //Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player 65 AttemptMove<Food>(xDir, yDir); 66 67 } 68 69 private void OnTriggerEnter2D(Collider2D other) 70 { 71 if(other.tag == "Food") 72 { 73 target = other.transform; 74 } 75 } 76 77 protected override void OnCantMove<T>(T component) 78 { 79 80 } 81 82} 83
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/18 15:30
2018/05/18 15:37 編集
退会済みユーザー
2018/05/18 15:48
2018/05/18 16:10
退会済みユーザー
2018/05/18 16:19
2018/05/18 16:24
退会済みユーザー
2018/05/18 16:32
2018/05/18 16:58
退会済みユーザー
2018/05/18 18:24
2018/05/19 06:20 編集
2018/05/19 06:49
退会済みユーザー
2018/05/19 08:30
2018/05/19 08:35
退会済みユーザー
2018/05/19 08:53 編集
退会済みユーザー
2018/05/19 09:00