
前提
こちらのサイトを見ながら2Dアクションを作っています。
https://dkrevel.com/makegame-beginner/make-2d-action-script-tidy/
このサイトを見ながら今まで書いたプログラムを整理していたのですが、このサイト通りに整理したら今までは問題なく動いていたのにエラーが発生するようになりました、どうすればよいでしょうか?
発生している問題・エラーメッセージ
Assets\player.cs(60,9): error CS0106: The modifier 'private' is not valid for this item Assets\player.cs(111,9): error CS0106: The modifier 'private' is not valid for this item Assets\player.cs(157,9): error CS0106: The modifier 'private' is not valid for this item
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class player : MonoBehaviour 6{ 7 private Rigidbody2D myRigidBody; 8 private bool isGround=false; 9 private Animator anim=null; 10 private Rigidbody2D rb=null; 11 private bool isjump=false; 12 private bool isHead=false; 13 private float jumpTime=0.0f; 14 private float jumpPos=0.0f; 15 private float dashTime=0.0f; 16 private float beforeKey=0.0f; 17 private bool isRun=false; 18 19 20 [Header("設置判定")]public GroundCheck ground; 21 [Header("頭をぶつけた判定")]public GroundCheck head; 22 [Header("移動速度")]public float speed; 23 [Header("重力")]public float gravity; 24 [Header("ジャンプ速度")]public float jumpSpeed; 25 [Header("ジャンプする高さ")]public float jumpHeight; 26 [Header("ジャンプ制限時間")]public float jumpLimitTime; 27 [Header("ダッシュの速さ表現")]public AnimationCurve dashCurve; 28 [Header("ジャンプの速さ表現")]public AnimationCurve jumpCurve; 29 30 31 32 33 void Start() 34 { 35 anim=GetComponent<Animator>(); 36 rb=GetComponent<Rigidbody2D>(); 37 } 38 39 40 void Update() 41 { 42 //設置判定を得る 43 isGround=ground.IsGround(); 44 isHead=head.IsGround(); 45 46 //各種座標軸の速度を求める 47 float xSpeed=GetXSpeed(); 48 float ySpeed=GetYSpeed(); 49 50 //アニメーションを適用 51 SetAnimation(); 52 53 //移動速度を設定 54 rb.velocity=new Vector2(xSpeed,ySpeed); 55 56 /// <summary> 57 /// Y成分で必要な計算をし、速度を返す 58 /// </summary> 59 /// <returns>Y軸の速さ</returns> 60 private float GetYSpeed() 61 { 62 float verticalKey=Input.GetAxisRaw("Vertical"); 63 float ySpeed=-gravity; 64 65 if(isGround && Input.GetButtonDown("Vertical")) 66 { 67 if(verticalKey>0) 68 { 69 ySpeed=jumpSpeed; 70 jumpPos=transform.position.y;//ジャンプした位置を記録する 71 isjump=true; 72 jumpTime=0.0f; 73 74 }else 75 { 76 isjump=false; 77 } 78 } 79 else if(isjump) 80 { 81 //spaceキーを押しているか 82 bool pushUpKey=verticalKey>0; 83 //現在の高さが飛べる高さより下か 84 bool canHeight=jumpPos+jumpHeight>transform.position.y; 85 //ジャンプ時間が長くなりすぎてないか 86 bool canTime=jumpLimitTime>jumpTime; 87 if(pushUpKey && canHeight && canTime &&! isHead) 88 { 89 ySpeed=jumpSpeed; 90 jumpTime+=Time.deltaTime; 91 } 92 else 93 { 94 isjump=false; 95 jumpTime=0.0f; 96 } 97 } 98 //アニメーションカーブを速度に適用 99 if(isjump) 100 { 101 ySpeed*=jumpCurve.Evaluate(jumpTime); 102 } 103 104 return ySpeed; 105 } 106 107 /// <summary> 108 /// X成分せ必要な計算をし、速度を返す 109 /// </summary> 110 /// <returns>X軸の速さ</returns> 111 private float GetXSpeed() 112 { 113 float horizontalKey=Input.GetAxisRaw("Horizontal"); 114 float xSpeed=0.0f; 115 116 if(horizontalKey>0) 117 { 118 transform.localScale=new Vector3(1,1,1); 119 isRun=true; 120 dashTime+=Time.deltaTime; 121 xSpeed=-speed; 122 } 123 else if(horizontalKey<0) 124 { 125 transform.localScale=new Vector3(-1,1,1); 126 isRun=true; 127 dashTime+=Time.deltaTime; 128 xSpeed=-speed; 129 } 130 else 131 { 132 isRun=false; 133 dashTime=0.0f; 134 xSpeed=0.0f; 135 } 136 137 //前回の入力からダッシュの反転を判断して速度を変える 138 if(horizontalKey>0 && beforeKey<0) 139 { 140 dashTime=0.0f; 141 } 142 else if(horizontalKey<0 && beforeKey>0) 143 { 144 dashTime=0.0f; 145 } 146 beforeKey=horizontalKey; 147 148 //アニメーションカーブを適用 149 xSpeed*=dashCurve.Evaluate(dashTime); 150 151 return xSpeed; 152 } 153 154 /// <summary> 155 /// アニメーションを設定する 156 /// </summary> 157 private void SetAnimation() 158 { 159 anim.SetBool("jumo",isjump); 160 anim.SetBool("ground",isGround); 161 anim.SetBool("run",isRun); 162 } 163 164 } 165} 166 167}
補足情報(FW/ツールのバージョンなど)
参考サイトでは62行目と113行目は
float verticalKey=Input.GetAxis("Vertical"); , float horizontalKey=Input.GetAxis("Horizontal");
こうなっていますが私は
float verticalKey=Input.GetAxisRaw("Vertical"); , float horizontalKey=Input.GetAxisRaw("Horizontal");
にしています、参考サイトコードだと一度ボタンを押しただけで二度飛んでしまったりとおかしな挙動になったので変えています
65行目も参考サイトでは
if(isGround)
になっていますが
if(isGround && Input.GetButtonDown("Vertical"))
に変更しています、ジャンプボタン長押しでもジャンプを一度だけにするために変更しています。
それ以外はサイトに搭載されているコードと一緒です。
回答3件
あなたの回答
tips
プレビュー