問題点
常に水平移動させる処理をnew Vectorを用いて考えたが、動いてくれない。ため、他の方法で考えるべきか、もしくは参考案があればご教授して頂きたい。
実装したいこと
水平移動する敵を実装し、Playerを検知した時にホーミングする動きをさせるようにしたい。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class EnemyMovement : MonoBehaviour 6{ 7 public float AutoSpped;//自動移動速度設定用変数 8 public Vector2 speed = new Vector2(0.05f, 0.05f); // 速度 9 public GameObject targetObject; // ターゲットとなるオブジェクト 10 11 private Rigidbody2D rg2d; 12 private float rad;// ラジアン変数 13 private Vector2 Position;// 現在位置を代入する為の変数 14 15 // Use this for initialization 16 void Start() 17 { 18 this.rg2d = GetComponent<Rigidbody2D>(); 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 EnemyMove(); 25 } 26 private void EnemyMove() 27 { 28 AutoSpped++; 29 rg2d.velocity = new Vector2(AutoSpped, rg2d.velocity.y); 30 } 31 32 private void OnCollisionEnter2D(Collision2D collision) 33 { 34 if (collision.gameObject.tag == "tsumiki") 35 { 36 gameObject.SetActive(false); 37 38 } 39 } 40 private void OnTriggerStay2D(Collider2D collision) 41 { 42 //Player発見し、Colliderに乗っている間、追尾ホーミング開始 43 if (collision.gameObject.name == "Player") 44 { 45 rad = Mathf.Atan2( 46 targetObject.transform.position.y - transform.position.y, 47 targetObject.transform.position.x - transform.position.x); 48 49 // 現在位置をPositionに代入 50 Position = transform.position; 51 // x += SPEED * cos(ラジアン) 52 // y += SPEED * sin(ラジアン) 53 // これで特定の方向へ向かって進んでいく。 54 Position.x += speed.x * Mathf.Cos(rad); 55 Position.y += speed.y * Mathf.Sin(rad); 56 // 現在の位置に加算減算を行ったPositionを代入する 57 transform.position = Position; 58 59 } 60 } 61} 62
このようにも考えてみたが、実装できなかった。
考え方がそもそもまちがっているのか…?
c#
1 2 // 敵現在座標 3 var Enemy_Pos = transform.localPosition; 4 // 敵現在座標 = 敵現在座標 * 敵移動速度 5 Enemy_Pos = Enemy_Pos * AutoSpped; 6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。