前提
unity2dで簡単な横スクロールアクションゲームを作っています。
なめらかではない横移動の実装についてです。
発生している問題
キーを離してからも0.5秒ほど動き続けてしまいます。入力の遅延はありません。
現在velocityを使っているのですが、それ以外のものを使ったほうが良ければそちらを使いたいと思っています。
該当のソースコード
c#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class playercontroler : MonoBehaviour { private Rigidbody2D i = null; public float speed; void Start() { i = GetComponent<Rigidbody2D>(); } void Update() { float xspeed = 0.0f; float key = Input.GetAxis("Horizontal"); if (key > 0) { transform.localScale = new Vector3(0.1f, 0.1f, 1); xspeed = speed; } else if (key < 0) { transform.localScale = new Vector3(-0.1f, 0.1f, 1); xspeed = -speed; } else { xspeed = 0.0f; } i.velocity = new Vector2(xspeed, i.velocity.y); } }
まだ回答がついていません
会員登録して回答してみよう