キャラクターにカプセルコライダーが入っています。タイトル通り坂道でキーを離し静止すると少しずつ下に下がってしまうのですがこれはどうすればいいのでしょうか?また提示画像から見て左や右に動かすと下に下がってしまいます解決法を押してくれますでしょうか?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { const float walk_speed_max = 2.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;//レイを飛ばす距離 const float ray_range = 1.0f;//レイを飛ばす距離 // 昇れる段差 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; private Vector3 pre_v; private Vector3 pre_dv; 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 (move.z != 0 && isGround == true) { if (Physics.Linecast(StepHit.position, StepHit.position + StepHit.forward * ray_range, out ray,LayerMask.GetMask("Slope"))) { //Debug.Log(Vector3.Angle(transform.up, ray.normal)); float angle = Vector3.Angle(transform.up, ray.normal); if ( angle <= slopeLimit) { v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, ray.normal) * transform.forward * s).y, 0f) + transform.forward * s; //前の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) * new Vector3(0,-1.0f,0) * s).y, 0f) + transform.forward * s; //前の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() { float s = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); // Debug.Log(s); //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(move); // 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
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。