質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1279閲覧

Rayで取得したオブジェクトの中心点から右に離れているか左に離れているかを検出する方法を知りたいです。

sunizen2000

総合スコア5

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/04/05 05:38

編集2020/04/05 06:07

前提・実現したいこと

Rayで取得したオブジェクトの中心点から右に離れているか左に離れているかを検出する方法を知りたいです。
用途としては壁走りを行う際、前方向にRayを飛ばし、あたったオブジェクトの中心点から右に離れているか左に離れているかを検出して、右なら右側に走るなどそんな感じに使う予定です。

該当のソースコード

ソースコードc# using System.Collections; using System.Collections.Generic; using UnityEngine; public class In_flont : MonoBehaviour//前方方向にRayを飛ばしあたったオブジェクトの中心から左か右かを取得するスプリクト { [SerializeField] private player player;//壁走りの処理やその他もろもろを行うスクリプト [SerializeField] private Ground Ground;//地面との当たり判定 [SerializeField] private Wall_Right Wall_Right;//壁との当たり判定(右) [SerializeField] private wall_left Wall_left;//壁との当たり判定(左) private float maxDistance = 0.9f;//Ray 長さ void Start() { } // Start is called before the first frame update void Update() { Ray ray = new Ray(transform.position, transform.forward); RaycastHit hit; Debug.DrawRay(ray.origin, ray.direction * maxDistance, Color.red); if (Physics.Raycast(ray, out hit, maxDistance)) { if (hit.collider.gameObject.tag == "wall" && !Ground.ground && player.moveSpeed == 10)  //壁を検出した時 && 地面に接触してない && 走る速さがその時10(MAX)なら { //たとえです if(中心から角度が右なら){ Wall_left.Left_Walls = true;//左に壁走り}          else{Wall_Right.Right_Walls = true;//右に壁走り} } } } }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class player : MonoBehaviour { [SerializeField] private Wall_Right Wall_Right;//壁との当たり判定(右) [SerializeField] private wall_left Wall_left;//壁との当たり判定(左) [SerializeField] private Ground Ground;//地面との衝突判定 private Player_Audio Audio;//プレイヤー音響 public Vector3 velocity;// 移動方向 private Vector3 Jump;//ジャンプ代入 public float moveSpeed;// 移動速度 private float walk_time;//歩いた時間 public Transform Rot;//Transform private Rigidbody rd;//Rigidbody private Vector3 roteto;//カメラ操作 private bool Landing; public bool ok , Gravitystop; //補助コード private float cameraZ; //壁走り時のカメラを斜めにする動作 // Use this for initialization void Start() { rd = GetComponent<Rigidbody>(); Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; Audio = GetComponent<Player_Audio>(); } // Update is called once per frame void Update() { if (Wall_left.Left_Walls || Wall_Right.Right_Walls) { ok = true; } else { ok = false;Gravitystop = true; } if (!ok) { Camera_Move(); Player_Move(); } if (Wall_Right.Right_Walls) { cameraZ = 15; Camera_Move_wall(); Rightwalk(); } if (Wall_left.Left_Walls) { cameraZ = -15; Camera_Move_wall(); Leftwalk(); } } void Camera_Move()//視点操作&視点可動域制限 { float X_Rotation = Input.GetAxis("Mouse X"); float Y_Rotation = Input.GetAxis("Mouse Y"); Rot.transform.Rotate(-Y_Rotation, X_Rotation, 0); Vector3 Y = Rot.transform.localEulerAngles; if (Y.x >= 180) Y.x -= 360; Rot.transform.localEulerAngles = new Vector3(Mathf.Clamp(Y.x, -50, 55), 0, 0); roteto = new Vector3(0, X_Rotation * 2, 0); rd.transform.eulerAngles += roteto; } void Camera_Move_wall()//視点操作&視点可動域制限 { float X_Rotation = Input.GetAxis("Mouse X"); float Y_Rotation = Input.GetAxis("Mouse Y"); Rot.transform.Rotate(-Y_Rotation, X_Rotation, 0); Vector3 Y = Rot.transform.localEulerAngles; if (Y.x >= 180) Y.x -= 360; if (Y.y >= 180) Y.y -= 360; Rot.transform.localEulerAngles = new Vector3(Mathf.Clamp(Y.x, -15, 15), Mathf.Clamp(Y.y, -15, 15), cameraZ); roteto = new Vector3(0, X_Rotation * 2, 0); Rot.transform.eulerAngles += roteto; } void Player_Move()//プレイヤー移動操作 { //重力 if (!Ground.ground) { Jump = Vector3.zero; Jump.y -= 10 * Time.deltaTime; rd.velocity += Jump; } //移動 velocity = Vector3.zero; if (Input.GetKey(KeyCode.W)) velocity.z += 1; if (Input.GetKey(KeyCode.A)) velocity.x -= 1; if (Input.GetKey(KeyCode.S)) velocity.z -= 1; if (Input.GetKey(KeyCode.D)) velocity.x += 1; velocity = gameObject.transform.rotation * velocity.normalized * moveSpeed * Time.deltaTime; if (velocity.magnitude > 0) //動いたら... { transform.position += velocity; //移動操作をプレイヤーに代入 //走り if (Input.GetKey(KeyCode.LeftShift) && moveSpeed <= 10 && Ground.ground) { moveSpeed += Time.deltaTime * 6; if (moveSpeed > 10) moveSpeed = 10; } if (!Input.GetKey(KeyCode.LeftShift) && moveSpeed >= 5) { moveSpeed -= 5 * Time.deltaTime * 6; if (moveSpeed < 5) moveSpeed = 5; } if (Input.GetKeyDown(KeyCode.Space) || !Ground.ground)//spaceキー押してないなら walk_time = 0; if (!Ground.ground && moveSpeed < 0)//壁走り解除後のスピード補正 moveSpeed = 5; else { //歩いている時間を計測(走ると倍) walk_time += Time.deltaTime * moveSpeed / 7; } } else { moveSpeed = 5; walk_time = 0; } //ジャンプ Jump = Vector3.zero; if (Input.GetKeyDown(KeyCode.Space) && Ground.ground) { Jump.y += 5; Audio.Player_Sound_janp(); } if (Ground.ground) { if (Landing) { Audio.Player_Sound_Landing(); Landing = false; } } else { Landing = true; } if (Jump.magnitude > 0) { rd.velocity += Jump; } if (walk_time > 0.5)//歩行時間が一定を超えたら { walk_time = 0; Audio.Player_Sound(); } } void Leftwalk() { ////////////////////////////初期設定(一度のみ)////////////////////////////////// if (Gravitystop) { Rot.transform.Rotate(new Vector3(0, 0, -15)); transform.rotation = Quaternion.AngleAxis(Wall_left.Wall_angle, new Vector3(0, 1, 0)); rd.isKinematic = true; Gravitystop = false; rd.isKinematic = false; } ////////////////////////////初期設定(一度のみ)////////////////////////////////// if (Input.GetKey(KeyCode.Space) && moveSpeed < 8)//space押されてないなら { } else { ///////////////////////////プレイヤーの動き///////////////////////////////////// velocity = Vector3.zero; if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) { velocity.z += 1; moveSpeed -= Time.deltaTime * 3; } else { moveSpeed = 0; } if (Input.GetKey(KeyCode.D)) velocity.x += 1; velocity = gameObject.transform.rotation * velocity.normalized * 10 * Time.deltaTime; if (velocity.magnitude > 0) //動いたら... { transform.position += velocity; //移動操作をプレイヤーに代入 } ///////////////////////////プレイヤーの動き///////////////////////////////////// ///////////////////////////走るサウンド///////////////////////////////////////// walk_time += Time.deltaTime * 2; if (walk_time > 0.5)//歩行時間が一定を超えたら { walk_time = 0; Audio.Player_Sound(); } ///////////////////////////走るサウンド///////////////////////////////////////// ///////////////////////////壁走りの上昇&下降/////////////////////////////////// Jump = Vector3.zero; if (moveSpeed > 5) { Jump.y += 1; Jump = gameObject.transform.rotation * Jump.normalized * Time.deltaTime * moveSpeed / 4; } if (moveSpeed < 5 && moveSpeed > 4) { Jump.y += 1; Jump = gameObject.transform.rotation * Jump.normalized * Time.deltaTime; } if (moveSpeed < 4 && moveSpeed > 3) { Jump.y -= 1; Jump = gameObject.transform.rotation * Jump.normalized * Time.deltaTime / 2; } if (moveSpeed < 3 && moveSpeed > 2) { Jump.y -= 1; Jump = gameObject.transform.rotation * Jump.normalized * Time.deltaTime; } if (moveSpeed < 2) { Jump.y -= 1; Jump = gameObject.transform.rotation * Jump.normalized * Time.deltaTime * 2; } if (Jump.magnitude > 0) //動いたら... { transform.position += Jump; //移動操作をプレイヤーに代入 } ///////////////////////////壁走りの上昇&下降/////////////////////////////////// } ///////////////////////////ジャンプ処理///////////////////////////////////////////// if (Input.GetKeyDown(KeyCode.Space)) {        //未実装 } ///////////////////////////ジャンプ処理///////////////////////////////////////////// if(moveSpeed < 0) { moveSpeed = 0; } } void Rightwalk() { //省略 } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

取得したオブジェクトのポジションから
プレイヤーのポジションを引けば
どの向きにオブジェクトがあるのかを求められます

投稿2020/04/05 10:33

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sunizen2000

2020/04/05 11:58

ありがとうございます、一度試してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問