坂道を上りり方向キーを離すと上に上がってしまう原因を解決したいです、また坂を下るときに動いてから重力が働き落下するという不自然な処理が出来上がってしまうのですがどうすればいいのでしょうか?
コリジョンはメッシュコライダーでuse gravityは切っています。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Rigidbody rb; Vector3 move; float input_h; float input_v; Vector3 move_x; Vector3 move_y; public GameObject gGround; ground spt_g; bool isJump; bool isDush; float jump_force = 200.0f; const float move_speed = 5.0f; Animator ani; // GameObject go = GameObject.Find("GameObject"); // 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; } // Update is called once per frame void Update() { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); move_y = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * move_speed; move_x = Camera.main.transform.right * input_h * move_speed; move = move_x + move_y; if(move != Vector3.zero) { transform.rotation = Quaternion.LookRotation(move.normalized); } if(Input.GetKey(KeyCode.RightControl)) { //isDush = true; } if(Input.GetKey(KeyCode.Space)) { if(spt_g.isGround == true && isJump == false) { isJump = true; } } Animation_Mng(); } /*アニメーション管理*/ void Animation_Mng() { float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); ani.SetFloat("Move_Speed",speed); } private void FixedUpdate() { rb.velocity = new Vector3(move.x, rb.velocity.y, move.z); if(isJump == true) { rb.AddForce(rb.velocity.x,jump_force,rb.velocity.z); isJump = false; // spt_g.isGround = false; } else { } if(spt_g.isGround == false) { rb.AddForce(new Vector3(0, -10.0f, 0)); } else { rb.AddForce(new Vector3(rb.velocity.x, 0, rb.velocity.z)); } if(input_h == 0 && input_v == 0) { //rb.velocity = new Vector3(0, rb.velocity.y, 0); } } private void OnCollisionEnter(Collision collision) { // Debug.Log("プレイヤーcollision"); } }
あなたの回答
tips
プレビュー