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() { } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。