問題
unityで2Dゲームを作っています。
プレイヤーを移動させることはできたのですが、移動した後に少し滑ってしまいます。
ピタッと移動した後にすぐ止まるようにしたいのですがうまくいきません。
参考にしたURL
こちらの質問は、3Dゲーム用の質問でした。
https://teratail.com/questions/105824
自分が考えたコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player : MonoBehaviour 6{ 7 public float m_speed; //プレイヤーの速さ 8 Rigidbody2D rbody; 9 10 void Start() 11 { 12 rbody = GetComponent<Rigidbody2D>(); //追加 13 } 14 15 void Update() 16 { 17 // 矢印キーの入力情報を取得する 18 var h = Input.GetAxis("Horizontal"); 19 var v = Input.GetAxis("Vertical"); 20 21 // 矢印キーが押されている方向にプレイヤーを移動する 22 var velocity = new Vector3(h, v) * m_speed; 23 transform.localPosition += velocity; 24 25 // 変数変更 26 rbody.AddForce(transform.right * Input.GetAxisRaw("Horizontal") * m_speed, (ForceMode2D)ForceMode.VelocityChange); 27 } 28 29 //止めたい時に呼び出し 30 void Stop() 31 { 32 rbody.velocity = Vector3.zero; 33 Vector3 zero = Vector3.zero; 34 rbody.angularVelocity = zero; 35 } 36} 37
エラーメッセージ
Assets\Scripts\Player.cs(34,33): error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'float'

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。