2D トップダウン シューターを作成しようとしていますが、レイキャストの方法がわかりません。2 番目のパラメーター (方向) に何を指定すればよいかわかりません。Input.mousePosition を試してみましたが、StackOverflow で見た他のいくつかのことを試しましたが、何も機能しないようです。これが私のプレイヤーキャラクターのコードです。
//Raycasts
var forward = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
Debug.DrawRay (transform.position, forward * 10, Color.green);
//Mouse Movement
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));
//Player Movement
if (Input.GetKey (KeyCode.D)) {rigidbody2D.AddForce(Vector3.right * speed);}
if (Input.GetKey (KeyCode.A)) {rigidbody2D.AddForce(Vector3.left * speed);}
if (Input.GetKey (KeyCode.W)) {rigidbody2D.AddForce(Vector3.up * speed);}
if (Input.GetKey (KeyCode.S)) {rigidbody2D.AddForce(Vector3.down * speed);}
レイキャストの方向部分を実行するために、スクリプトのマウス移動部分から再利用できるものはありますか?
助けてくれてありがとう!