坂道を上りキーを離したときにその分? 上に上がる力が働いてるみたいなのですがこれはなぜでしょうか?FixedUpdateに変えてみたのですが
原因がわかりません。教えてくれますでしょうか?
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 const float gravity_force = -10.0f; private const float jump_force = 50.0f; private float gravity;//ジャンプの上下数値管理 private Animator ani; public Transform ray; private RaycastHit hit; // Use this for initialization void Start () { walk_speed = 50.0f; rb = GetComponent<Rigidbody>(); ani = GetComponent<Animator>(); } // Update is called once per frame void Update () { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); 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.GetKey(KeyCode.Space)) { gravity = jump_force; } else { gravity = gravity_force; } /*アニメーション*/ float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); //float speed = 10.0f; ani.SetFloat("Walk_Speed",speed); // ani.speed(speed / 3); /*坂道処理*/ Debug.Log(rb.velocity); } void FixedUpdate() { if (Physics.Linecast(ray.position, ray.position + move, out hit) == true) { //Debug.Log("ヒット"); // transform.rotation = Quaternion.FromToRotation(Vector3.up, ray.normal); Vector3 v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.forward * walk_speed).y, 0f) + transform.forward * walk_speed; move = v; //rb.velocity = new Vector3(rb.velocity.x, v.y ,rb.velocity.z); } else { } rb.AddForce(move + new Vector3(0, gravity, 0)); //Debug.Log(rb.velocity); } private void OnCollisionEnter(Collision collision) { gravity = 0; rb.velocity = new Vector3(0, 0, 0); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/09/30 14:58
2019/09/30 15:16
退会済みユーザー
2019/10/02 23:25