前提・実現したいこと
velocityで移動制御させたいのですが、コードが意図した通りに動かない為、
ご教示お願いします。
こちらのサイトを参考にしました。
やりたいこととしては、
・入力受付はUpdateで行いたい。
・VerticalやHorizonなどの入力でなく、左キー、右キーでの入力。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Cube : MonoBehaviour { 6 7 Rigidbody rb; 8 bool isRightPress = false; 9 bool isLeftPress = false; 10 float speed = 10.0f; 11 12 void Start () { 13 rb = GetComponent<Rigidbody>(); 14 } 15 16 void Update () { 17 if(Input.GetKey(KeyCode.RightArrow)){ 18 isRightPress = true; 19 }else if(Input.GetKeyUp(KeyCode.RightArrow)){ 20 isRightPress = false; 21 } 22 if(Input.GetKey(KeyCode.LeftArrow)){ 23 isLeftPress = true; 24 }else if(Input.GetKeyUp(KeyCode.LeftArrow)){ 25 isLeftPress = false; 26 } 27 28 } 29 30 void FixedUpdate () { 31 if(isRightPress){ 32 rb.velocity = Vector3.right * speed; 33 } 34 if(isLeftPress){ 35 rb.velocity = Vector3.left * speed; 36 } 37 if(!isRightPress){ 38 rb.velocity = Vector3.zero; 39 } 40 } 41} 42
左キーを押したときに動かないです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/10 02:29