方向キーを押してAddForce()でキャラクターが加速してボタンを離したときに動いてたキャラクターが少しずつ減速すると思うんですけどその減速する速度を調節したいのですがどうすればいいのでしょうか?
rigidbody2D.velocity = Vector2.zeroとすれば止めることはできますが自分がしたいことはその速度を調整したいのです。
public class PlayerController : MonoBehaviour { public float JumpForce = 250.0f; public float max_speed = 50f; public float speedForce = 12.0f; private bool isJump; private bool isRun; private bool isGround; private Animator animator; private Rigidbody2D rb; [SerializeField] ContactFilter2D filter2d; // Use this for initialization void Start () { rb = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); isJump = false; isRun = false; // isGround = false; } private void FixedUpdate() { //地面チェック if (GetComponent<Rigidbody2D>().IsTouching(filter2d)) { isJump = false; // isGround = true; } } // Update is called once per frame void Update () { int key = 0; //if(transform.position.y < ) if(Input.GetKey(KeyCode.Space))//スペースキージャンプ入力 { if (!isJump) { //GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce)); GetComponent<Rigidbody2D>().AddForce(transform.up * JumpForce); animator.SetBool("Jump",true); isJump = true; } } if(isJump == false)//ジャンプしてないときはジャンプモーションを消す。 { animator.SetBool("Jump",false); } float speedx = Mathf.Abs(rb.velocity.x);//移動速度を監視 if (Input.GetKey(KeyCode.RightArrow))//右キー入力 { key = 1; if (speedx < max_speed) { rb.AddForce( transform.right * key * speedForce); } else { //GetComponent<Rigidbody2D>().AddForce(transform.right * key * speed); } if (isRun == false && isJump == false) { animator.SetBool("Run",true); } } Debug.Log(rb.velocity.x); if (Input.GetKey(KeyCode.LeftArrow))//左キー入力 { key = -1; if (speedx < max_speed) { rb.AddForce( transform.right * key * speedForce); } else { //GetComponent<Rigidbody2D>().AddForce(transform.right * key * speed); } if (isRun == false && isJump == false) { animator.SetBool("Run",true); } } if(key == 0 || isJump == true) { isRun = false; animator.SetBool("Run",false); } if(key != 0) { transform.localScale = new Vector3(key,1,1); } } private void OnCollisionEnter2D(Collision2D collision) { isJump = false; // isGround = true; } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/26 01:22
2019/03/26 01:45
退会済みユーザー
2019/03/26 03:03
2019/03/26 03:07