前提
Unityで上下左右にダッシュジャンプのようなものができるような2Dアクションゲームを作っています。
条件分離が上手く行きません。
発生している問題・エラーメッセージ
Assets\Scripts\PlayerManager.cs(36,49): error CS1513: } expected Assets\Scripts\PlayerManager.cs(37,26): error CS1002: ; expected Assets\Scripts\PlayerManager.cs(44,50): error CS1513: } expected Assets\Scripts\PlayerManager.cs(45,25): error CS1002: ; expected
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerManager : MonoBehaviour 6{ 7 [SerializeField] LayerMask blockLayer; 8 9 public enum DIRECTION_TYPE 10 { 11 STOP, 12 LEFT, 13 RIGHT, 14 } 15 DIRECTION_TYPE direction = DIRECTION_TYPE.STOP; 16 float speed; 17 Rigidbody2D rigidbody2D; 18 19 float jumpPower = 400; 20 float jumpdashPower = 400; 21 22 void Start() 23 { 24 rigidbody2D = GetComponent<Rigidbody2D>(); 25 } 26 27 void Update() 28 { 29 float x = Input.GetAxis("Horizontal"); 30 if (x == 0) 31 { 32 direction = DIRECTION_TYPE.STOP; 33 } 34 else if (Input.GetKey("space") && (x > 0)) 35 { 36 direction = DIRECTION_TYPE.LEFTJUMP; 37 else (x < 0) 38 { 39 direction = DIRECTION_TYPE.LEFT; 40 } 41 } 42 else if (Input.GetKey("space") && (x < 0)) 43 { 44 direction = DIRECTION_TYPE.RIGHTJUMP; 45 else (x > 0) 46 { 47 direction = DIRECTION_TYPE.RIGHT; 48 } 49 } 50 51 if (Input.GetKey("up")) 52 { 53 if (Input.GetKeyDown("space")) 54 { 55 Jump(); 56 } 57 } 58 if (Input.GetKey("down")) 59 { 60 if (Input.GetKeyDown("space")) 61 { 62 DownJump(); 63 } 64 } 65 } 66 67 void FixedUpdate() 68 { 69 switch (direction) 70 { 71 case DIRECTION_TYPE.STOP: 72 speed = 0; 73 break; 74 case DIRECTION_TYPE.RIGHT: 75 speed = 3; 76 transform.localScale = new Vector3(1, 1, 1); 77 break; 78 case DIRECTION_TYPE.LEFT: 79 speed = -3; 80 transform.localScale = new Vector3(-1, 1, 1); 81 break; 82 case DIRECTION_TYPE.RIGHTJUMP: 83 speed = 30; 84 transform.localScale = new Vector3(1, 1, 1); 85 break; 86 case DIRECTION_TYPE.LEFTJUMP: 87 speed = -30; 88 transform.localScale = new Vector3(-1, 1, 1); 89 break; 90 } 91 rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); 92 } 93 94 void Jump() 95 { 96 rigidbody2D.AddForce(Vector2.up * jumpPower); 97 } 98 void DownJump() 99 { 100 rigidbody2D.AddForce(Vector2.down * jumpPower); 101 } 102 103 104 bool IsGround() 105 { 106 Vector3 leftStartPoint = transform.position - transform.right * 0.3f; 107 Vector3 endPoint = transform.position - transform.up * 0.1f; 108 Vector3 rightStartPoint = transform.position + transform.right * 0.3f; 109 110 // 確認用 111 Debug.DrawLine(leftStartPoint, endPoint); 112 Debug.DrawLine(rightStartPoint, endPoint); 113 114 return Physics2D.Linecast(leftStartPoint, endPoint, blockLayer) || Physics2D.Linecast(rightStartPoint, endPoint, blockLayer); 115 } 116}
実現したいこと
方向キー+スペースキーでその方向にダッシュジャンプのようなものを行えるようにしたいです。
また、設置判定を用いて地面にいる際に行うジャンプと空中にいる際のジャンプを分けて実行し、モーションなどを分けたいです。
補足情報(FW/ツールのバージョンなど)
初めは上下左右のジャンプと同じように↓
void Jump() { rigidbody2D.AddForce(Vector2.up * jumpPower); } void DownJump() { rigidbody2D.AddForce(Vector2.down * jumpPower); }
左右のダッシュジャンプも行おうと思っていたのですがx軸はrigidbodyを使わずにプレイヤーを動かしているため↓
rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);
上手く動作してくれず左右の移動と同じような形で瞬間的にspeedを上げる方法にすることにしました。
Unityのバージョンは2019.3.12
初心者なので優しく教えていただけると嬉しいですm(__)m
回答3件
あなたの回答
tips
プレビュー