c#
1 void Update () { 2 var prevPos = transform.position; 3 4 float x = this.transform.position.x - prevPos.x; 5 float y = this.transform.position.y - prevPos.y; 6 7 Vector2 vec = new Vector2(x, y).normalized; 8 9 10 11 float rot = Mathf.Atan2(vec.y, vec.x) * 180 / Mathf.PI; 12 if (rot > 180) rot -= 360; 13 if (rot < -180) rot += 360; 14 15 16 17 Debug.Log("Angle = " + rot); 18 19 prevPos = this.transform.position; 20 } 21 private void FixedUpdate() 22 { 23 PlayerMove(); 24 } 25 void PlayerMove() 26 { 27 //移動処理 28 float dy = Input.GetAxis("Vertical") * Playerspped; 29 float dx = Input.GetAxis("Horizontal") * Playerspped; 30 31 rb2d.velocity = new Vector2(dx, dy); 32 // ShootNWay(,bulletSpeed,BulletCount) 33 34 35 } 36
オブジェクトの角度を求め、キーの入力時角度を入力先に向くようにしようとしました。が、実装できず。
Debu.LogでAngleを出力しようとしたのですが、rotが0のままで動きません。この場合は、GetAxisの下に書けば良いのでしょうか・・・?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/25 22:16
2018/06/26 00:25