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

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

新規登録して質問してみよう
ただいま回答率
85.51%
Unity3D

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

Q&A

解決済

1回答

1054閲覧

キャラクターの真下にRayを飛ばしたいが謎の方向に線が伸びてしまう。

退会済みユーザー

退会済みユーザー

総合スコア0

Unity3D

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

0グッド

0クリップ

投稿2019/10/04 14:55

ray_update()関数コメント部のコードなのですがなぜrayが謎の方向に行くのでしょうか?自分はキャラクターの足から下に飛ばして地面判定に使いたいのですが調べても情報がなく困っています。

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private float input_h; private float input_v; private Vector3 move; private Rigidbody rb; private float walk_speed;//移動速度 private float run_speed;//移動速度 private const float gravity_force = -50.0f; private const float jump_force = 100.0f; private float gravity;//ジャンプの上下数値管理 private Animator ani; private bool isJump; private bool isGround; // Use this for initialization void Start() { walk_speed = 100.0f; run_speed = 50.0f; isJump = false; isGround = false; rb = GetComponent<Rigidbody>(); ani = GetComponent<Animator>(); } // Update is called once per frame void Update() { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); if (Input.GetKey(KeyCode.X) && walk_speed <= 250) { walk_speed += run_speed; } else { walk_speed = 100.0f; } Vector3 move_x = new Vector3(); Vector3 move_y = new Vector3(); move_y = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * walk_speed; move_x = Camera.main.transform.right * input_h * walk_speed; move = move_x + move_y; if (move != Vector3.zero) { transform.rotation = Quaternion.LookRotation(move.normalized); } /*ジャンプ処理*/ if (Input.GetKeyDown(KeyCode.Space) && isJump == false && isGround == true) { isJump = true; isGround = false; gravity = jump_force; move.y = gravity; } else { //gravity += gravity_force; // move.y = gravity_force; } /*アニメーション*/ float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); Debug.Log(speed); // Debug.Log(rb.velocity.y); //float speed = 10.0f; ani.SetFloat("Walk_Speed", speed); if (gravity > 1) { ani.SetFloat("isJump", gravity); } if (isJump == true) { //ani.SetFloat("isJump", Mathf.Abs(gravity)); } // ani.speed(speed / 3); /*坂道処理*/ // Debug.Log(rb.velocity); ray_update(); } /////////////////////////////////////////////////////////////////////////////////////////////// void ray_update() { Ray ray = new Ray(transform.position, transform.position - new Vector3(0, -1, 0)); RaycastHit rayhit; int range = -10; Debug.DrawLine(ray.origin, ray.direction, Color.red); if(Physics.Raycast(ray,out rayhit,range)) { Debug.Log("ヒット!"); } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private float input_h; private float input_v; private Vector3 move; private Rigidbody rb; private float walk_speed;//移動速度 private float run_speed;//移動速度 private const float gravity_force = -50.0f; private const float jump_force = 100.0f; private float gravity;//ジャンプの上下数値管理 private Animator ani; private bool isJump; private bool isGround; // Use this for initialization void Start() { walk_speed = 100.0f; run_speed = 50.0f; isJump = false; isGround = false; rb = GetComponent<Rigidbody>(); ani = GetComponent<Animator>(); } // Update is called once per frame void Update() { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); if (Input.GetKey(KeyCode.X) && walk_speed <= 250) { walk_speed += run_speed; } else { walk_speed = 100.0f; } Vector3 move_x = new Vector3(); Vector3 move_y = new Vector3(); move_y = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * walk_speed; move_x = Camera.main.transform.right * input_h * walk_speed; move = move_x + move_y; if (move != Vector3.zero) { transform.rotation = Quaternion.LookRotation(move.normalized); } /*ジャンプ処理*/ if (Input.GetKeyDown(KeyCode.Space) && isJump == false && isGround == true) { isJump = true; isGround = false; gravity = jump_force; move.y = gravity; } else { //gravity += gravity_force; // move.y = gravity_force; } /*アニメーション*/ float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); Debug.Log(speed); // Debug.Log(rb.velocity.y); //float speed = 10.0f; ani.SetFloat("Walk_Speed", speed); if (gravity > 1) { ani.SetFloat("isJump", gravity); } if (isJump == true) { //ani.SetFloat("isJump", Mathf.Abs(gravity)); } // ani.speed(speed / 3); /*坂道処理*/ // Debug.Log(rb.velocity); ray_update(); } ////////////////////////////////////////////////////////////////////////////////// void ray_update() { Ray ray = new Ray(transform.position, transform.position - new Vector3(0, -1, 0)); RaycastHit rayhit; int range = -10; Debug.DrawLine(ray.origin, ray.direction, Color.red); if(Physics.Raycast(ray,out rayhit,range)) { Debug.Log("ヒット!"); } } ////////////////////////////////////////////////////////////////////////////// void FixedUpdate() { rb.AddForce(move); } private void OnCollisionEnter(Collision collision) { isGround = true; isJump = false; gravity = 0; move.y = 0; rb.velocity = new Vector3(0, 0, 0); } void Hit() { } } void FixedUpdate() { rb.AddForce(move); } private void OnCollisionEnter(Collision collision) { isGround = true; isJump = false; gravity = 0; move.y = 0; rb.velocity = new Vector3(0, 0, 0); } void Hit() { } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

Debug.LinecastをDebug.DrawRayにしたら解決しました。表示に問題があったみたいです。

投稿2019/10/04 15:13

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問