Q&A
前提
unity2dで簡単な横スクロールアクションゲームを作っています。
なめらかではない横移動の実装についてです。
発生している問題
キーを離してからも0.5秒ほど動き続けてしまいます。入力の遅延はありません。
現在velocityを使っているのですが、それ以外のものを使ったほうが良ければそちらを使いたいと思っています。
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class playercontroler : MonoBehaviour 6{ 7 private Rigidbody2D i = null; 8 public float speed; 9 10 void Start() 11 { 12 i = GetComponent<Rigidbody2D>(); 13 } 14 15 16 void Update() 17 { 18 float xspeed = 0.0f; 19 float key = Input.GetAxis("Horizontal"); 20 if (key > 0) 21 { 22 transform.localScale = new Vector3(0.1f, 0.1f, 1); 23 xspeed = speed; 24 } 25 else if (key < 0) 26 { 27 transform.localScale = new Vector3(-0.1f, 0.1f, 1); 28 xspeed = -speed; 29 } 30 else 31 { 32 xspeed = 0.0f; 33 } 34 i.velocity = new Vector2(xspeed, i.velocity.y); 35 } 36} 37
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/05/19 10:49