提示コード//////コメントの中のコードです。坂道を上に登った時の処理はif文で完成したいのですが
質問1 "坂を下る時"の処理の実装方法が知りたいです。
質問2 坂道を上に登る時と下る時の処理を分ける分岐の実装法が知りたいまた、坂道で斜めに進む時動けなくなるのでその辺も考慮した処理の実装法が知りたい
質問4 平地から坂道の上ろうとすると引っ掛かって登れない原因が知りたい
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { const float walk_speed_max = 20.0f; Rigidbody rb; Vector3 move; float input_h; float input_v; Vector3 move_x; Vector3 move_z; public GameObject gGround; ground spt_g; bool isJump; bool isDush; bool isRay; bool isGround = false; float jump_force = 200.0f; float move_speed = walk_speed_max; Animator ani; private Transform StepHit; private RaycastHit downRay; private Transform Hit; private RaycastHit ray; const float ray_range = 0.3f;//レイを飛ばす距離 // 昇れる段差 private float stepOffset = 0.3f; //坂道の角度登れる private float slopeLimit = 65f; // 昇れる段差の位置から飛ばすレイの距離 private float slopeDistance = 0.1f; private Vector3 v; // GameObject go = GameObject.Find("GameObject"); private Transform prev; void move_step() { //y軸は逆にしてるプラスが下 Debug.DrawLine(StepHit.position, StepHit.position + StepHit.right * ray_range, Color.red); Debug.DrawLine(StepHit.position, StepHit.position + StepHit.forward * ray_range, Color.blue); float s = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); if (s > 0 && isGround == true) { if (Physics.Linecast(StepHit.position, StepHit.position + StepHit.forward * ray_range, out ray,LayerMask.GetMask("Slope"))) // if (Physics.Linecast(StepHit.position, StepHit.position + StepHit.forward * ray_range, out ray)) { //Debug.Log(Vector3.Angle(transform.up, ray.normal)); if (Vector3.Angle(transform.up, ray.normal) <= slopeLimit) { v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, ray.normal) * transform.forward * s).y, 0f) + transform.forward * s; Debug.Log("上がる"+v.y); move.y += v.y; } ////////////////////////////////////////////////////////////////////////////////////////////////////// } else if (Physics.Linecast(StepHit.position, StepHit.position + StepHit.right * ray_range, out downRay, LayerMask.GetMask("Slope"))) { Debug.Log(Vector3.Angle(transform.up, downRay.normal)); if (Vector3.Angle(transform.up, downRay.normal) <= slopeLimit) { Debug.Log("下がる"); v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, ray.normal) * transform.forward * s).y, 0f) + transform.up * s; Debug.Log("下がる:" + v.y); move.y += -v.y; } // Debug.Log("下がるとき"); } ////////////////////////////////////////////////////////////////////////////////////////////////////// } } // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); spt_g = gGround.GetComponent<ground>(); ani = GetComponent<Animator>(); isJump = false; isDush = false; StepHit = transform.Find("StepRay"); } // Update is called once per frame void Update() { //Debug.Log(transform.up); // Debug.Log(transform.right); isGround = spt_g.isGround; // Debug.Log(isGround); //input_h = Input.GetAxis("Horizontal"); //input_v = Input.GetAxis("Vertical"); if(Input.GetKey(KeyCode.LeftArrow)) { input_h = -1; }else if (Input.GetKey(KeyCode.RightArrow)) { input_h = 1; } else { input_h = 0; } if (Input.GetKey(KeyCode.UpArrow)) { input_v = 1; } else if (Input.GetKey(KeyCode.DownArrow)) { input_v = -1; } else { input_v = 0; } move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v; move_x = Camera.main.transform.right * input_h; move = move_x + move_z; move.x *= move_speed; move.z *= move_speed; // Debug.Log(isRay); if (move != Vector3.zero) { transform.rotation = Quaternion.LookRotation(move.normalized); } // /*走る*/ if(Input.GetKey(KeyCode.RightControl)) { isDush = true; move_speed = 15.0f; } else { isDush = false; move_speed = walk_speed_max; } if(Input.GetKey(KeyCode.Space)) { if(isGround == true && isJump == false) { isJump = true; } } move_step(); Animation_Mng(); } /*アニメーション管理*/ void Animation_Mng() { float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); ani.SetFloat("Move_Speed",speed); ani.SetBool("isDush",isDush); //ani.speed = 2; } private void FixedUpdate() { if (isGround == false) { rb.AddForce(new Vector3(0, -30.0f, 0)); rb.velocity = new Vector3(move.x, rb.velocity.y, move.z); } else { rb.velocity = new Vector3(move.x, move.y, move.z); } } private void OnCollisionEnter(Collision c) { //isGround = true; } private void OnCollisionExit(Collision c) { //isGround = false; // Debug.Log("プレイヤーcollision false"); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/10/31 08:22
2019/10/31 10:14
退会済みユーザー
2019/10/31 10:47
2019/10/31 11:58
2019/10/31 12:06
退会済みユーザー
2019/10/31 12:28
2019/10/31 12:42
退会済みユーザー
2019/10/31 12:49
退会済みユーザー
2019/10/31 13:11
2019/10/31 14:16
退会済みユーザー
2019/10/31 14:19