質問内容
Unity2Dにて、プレイヤーのムーブメントを実装したのですが、
壁に向かって動いた際にガクガクと壁にめり込んでしまい、行動不可能になってしまいます。
このような感じでめり込みます→Gyazo GIF
解決方法のひとつとして、target
のタイルが当たり判定を持っているかの可否でvoid Move()
メソッドを呼び出すという方法が挙げられたのですが、具体的な実装方法も分かりません。
該当のソースコード
C#
1Vector3 MOVEX = new Vector3(1.0f, 0, 0); 2 Vector3 MOVEY = new Vector3(0, 1.0f, 0); 3 4 float step = 4f; 5 Vector3 target; 6 Vector3 prevPos; 7 8 Animator animator; 9 Tilemap tile; 10 11 void Start() 12 { 13 target = transform.position; 14 animator = GetComponent<Animator>(); 15 16 var tilemap = GetComponent<Tilemap>(); 17 tilemap = tile; 18 } 19 20 void Update() 21 { 22 if (transform.position == target) 23 { 24 SetTargetPosition(); 25 } 26 Move(); 27 } 28 29 void FixedUpdate() 30 { 31 32 } 33 34 void SetTargetPosition() 35 { 36 37 prevPos = target; 38 39 if (Input.GetKey(KeyCode.RightArrow)) 40 { 41 target = transform.position + MOVEX; 42 SetAnimationParam(1); 43 return; 44 } 45 if (Input.GetKey(KeyCode.LeftArrow)) 46 { 47 target = transform.position - MOVEX; 48 SetAnimationParam(2); 49 return; 50 } 51 if (Input.GetKey(KeyCode.UpArrow)) 52 { 53 target = transform.position + MOVEY; 54 SetAnimationParam(3); 55 return; 56 } 57 if (Input.GetKey(KeyCode.DownArrow)) 58 { 59 target = transform.position - MOVEY; 60 SetAnimationParam(0); 61 return; 62 } 63 } 64 65 void SetAnimationParam(int param) 66 { 67 animator.SetInteger("", param); 68 } 69 70 void Move() 71 { 72 transform.position = Vector3.MoveTowards(transform.position, target, step * Time.deltaTime); 73 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/03 11:08