質問するログイン新規登録

回答編集履歴

3

修正

2020/01/16 08:23

投稿

Y0241-N
Y0241-N

スコア1066

answer CHANGED
@@ -36,7 +36,7 @@
36
36
  private float horizontalKey;
37
37
  private Animator anim = null;
38
38
  private Rigidbody2D rb = null;//アニメとRBの変数宣言
39
- private string ochaTag = "Player";//★お茶タグを宣言
39
+ private string ochaTag = "Ocha";//★お茶タグを宣言
40
40
  private bool isKasoku = false;//★加速boolを宣言、通常は加速してないよ(お茶を取ってないよ)
41
41
 
42
42
  void Start()
@@ -56,12 +56,12 @@
56
56
  {
57
57
  if(verticalKey < 0)//下キーが押されたら(上に行く必要がないゲームなので下キーのみ指定しています)
58
58
  {
59
- //anim.SetBool("run", true);//アニメon
59
+ anim.SetBool("run", true);//アニメon
60
60
  ySpeed = -Speed;//走る
61
61
  }
62
62
  else
63
63
  {
64
- //anim.SetBool("run", false);//アニメoff
64
+ anim.SetBool("run", false);//アニメoff
65
65
  ySpeed = 0.0f;//走らない
66
66
  }
67
67
 

2

ミス修正

2020/01/16 08:22

投稿

Y0241-N
Y0241-N

スコア1066

answer CHANGED
@@ -97,7 +97,6 @@
97
97
  {
98
98
  if(collision.gameObject.tag == ochaTag)//★お茶タグとぶつかったら
99
99
  {
100
- Debug.Log("HIT");
101
100
  StartCoroutine(Kasoku());
102
101
  Destroy(collision.gameObject);//お茶が消えるよ
103
102
  }

1

ミス修正

2020/01/16 01:29

投稿

Y0241-N
Y0241-N

スコア1066

answer CHANGED
@@ -1,3 +1,8 @@
1
+ 【修正2020/01/16】スクリプトを修正しました、変更点は移動の定義を個別に分け、加速度や加速時間をインスペクターから変更できるように追加しておきました。
2
+ 個人的な感想ですが、加速時間5秒は体感的に長かったので3秒ぐらいでいいかもしれません。
3
+
4
+ -----
5
+
1
6
  やるとしたらアイテム取得時に加速度フラグを4秒間Trueにして4秒たったらFalseにすればいいと思いますが、その切り替えをFixedUpdateの中に書いているのはよくありません。
2
7
  何故かというと、これは条件を満たしている時に常に適応されるので、加速度フラグがFalseの時、毎フレーム「rb.velocity = new Vector2(xSpeed, ySpeed);」「rb.velocity = new Vector2(xSpeed, ySpeed);」「rb.velocity = new Vector2(xSpeed, ySpeed);」...と繰り返されていることになります。
3
8
 
@@ -22,12 +27,16 @@
22
27
 
23
28
  public class player : MonoBehaviour
24
29
  {
25
- public float speed;//インスペクタで速度をいじくる宣言
30
+ public float Speed;//インスペクタで速度をいじくる宣言
31
+ public float KasokuSpeed = 1;//インスペクターでお好みの加速値を決めてください、0にすると移動しなくなるので1以上の値にしてください。
32
+ public int KasouTime = 1;//インスペクターでお好みの倍速にする時間を決めてください、0にすると加速しなくなるので1以上の値にしてください。
26
33
  private float ySpeed = 0.0f;//y軸移動速度の初期値
27
34
  private float xSpeed = 0.0f;//x軸移動速度の初期値
35
+ private float verticalKey;
36
+ private float horizontalKey;
28
37
  private Animator anim = null;
29
38
  private Rigidbody2D rb = null;//アニメとRBの変数宣言
30
- private string ochaTag = "Ocha";//★お茶タグを宣言
39
+ private string ochaTag = "Player";//★お茶タグを宣言
31
40
  private bool isKasoku = false;//★加速boolを宣言、通常は加速してないよ(お茶を取ってないよ)
32
41
 
33
42
  void Start()
@@ -38,36 +47,39 @@
38
47
 
39
48
  void FixedUpdate()
40
49
  {
41
- float verticalKey = Input.GetAxis("Vertical");//上下キー宣言
50
+ verticalKey = Input.GetAxis("Vertical");//上下キー宣言
42
- float horizontalKey = Input.GetAxis("Horizontal");//左右キー宣言
51
+ horizontalKey = Input.GetAxis("Horizontal");//左右キー宣言
52
+ ChangeSpeed();
53
+ }
43
54
 
55
+ void ChangeSpeed()
56
+ {
44
57
  if(verticalKey < 0)//下キーが押されたら(上に行く必要がないゲームなので下キーのみ指定しています)
45
58
  {
46
- anim.SetBool("run", true);//アニメon
59
+ //anim.SetBool("run", true);//アニメon
47
- ySpeed = -speed;//走る
60
+ ySpeed = -Speed;//走る
48
61
  }
49
62
  else
50
63
  {
51
- anim.SetBool("run", false);//アニメoff
64
+ //anim.SetBool("run", false);//アニメoff
52
65
  ySpeed = 0.0f;//走らない
53
66
  }
54
67
 
55
68
  if(horizontalKey > 0)//右キーが押されたら
56
69
  {
57
- xSpeed = speed;//右に行く
70
+ xSpeed = Speed;//右に行く
58
71
  }
59
72
  else if(horizontalKey < 0)//左キーが押されたら
60
73
  {
61
- xSpeed = -speed;//左に行く
74
+ xSpeed = -Speed;//左に行く
75
+ }else if(horizontalKey == 0)
76
+ {
77
+ xSpeed = 0f;
62
78
  }
63
- rb.velocity = new Vector2(xSpeed, ySpeed);
64
- }
65
79
 
66
- void ChangeSpeed()
67
- {
68
80
  if(isKasoku==true)//★加速フラグがtrueの場合の挙動
69
81
  {
70
- rb.velocity = new Vector2(xSpeed,ySpeed*4);//★y軸移動速度が4倍になる(今後通常時の走行速度の違う他キャラも増やすので、この方が汎用性があります)
82
+ rb.velocity = new Vector2(xSpeed,ySpeed * KasokuSpeed);//★y軸移動速度が4倍になる(今後通常時の走行速度の違う他キャラも増やすので、この方が汎用性があります)
71
83
  }
72
84
  else if(isKasoku==false)//★加速フラグがfalse、つまり通常時
73
85
  {
@@ -78,15 +90,14 @@
78
90
  IEnumerator Kasoku()
79
91
  {
80
92
  isKasoku = true;//★加速フラグがtrueになるよ
81
- ChangeSpeed();
82
- yield return new WaitForSeconds(5);
93
+ yield return new WaitForSeconds(KasouTime);
83
94
  isKasoku = false;
84
- ChangeSpeed();
85
95
  }
86
96
  private void OnCollisionEnter2D(Collision2D collision)//★(お茶との)衝突判定
87
97
  {
88
- if(collision.collider.tag == ochaTag)//★お茶タグとぶつかったら
98
+ if(collision.gameObject.tag == ochaTag)//★お茶タグとぶつかったら
89
99
  {
100
+ Debug.Log("HIT");
90
101
  StartCoroutine(Kasoku());
91
102
  Destroy(collision.gameObject);//お茶が消えるよ
92
103
  }