// Update is called once per frame void Update() { if (GameManagement.Instance.isGameStart) { // get keyboard input // Because the character can move in one direction in the same time, // so change the other directions as 'else' if (Input.GetKey(KeyCode.RightArrow)) // right { Vector2 dest = rb.position + Vector2.right * moveSpeed; rb.MovePosition(dest); animator.ChangeDir(SelfAnim.Anim_Dir.Right); } else if (Input.GetKey(KeyCode.LeftArrow)) // left { Vector2 dest = rb.position + Vector2.left * moveSpeed; rb.MovePosition(dest); animator.ChangeDir(SelfAnim.Anim_Dir.Left); } else if (Input.GetKey(KeyCode.UpArrow)) // up { Vector2 dest = rb.position + Vector2.up * moveSpeed; rb.MovePosition(dest); animator.ChangeDir(SelfAnim.Anim_Dir.Up); } else if (Input.GetKey(KeyCode.DownArrow)) // down { Vector2 dest = rb.position + Vector2.down * moveSpeed; rb.MovePosition(dest); animator.ChangeDir(SelfAnim.Anim_Dir.Down); } } }
// Update is called once per frame void Update() { if (GameMgr.Instance.isStartGame) { if (Input.GetKey(KeyCode.W)) { Vector2 dest = rigidbody2.position + Vector2.up * moveSpeed / 10; rigidbody2.MovePosition(dest); animator.ChangeDir(SelfAnim.MoveDirAnim.Up); } else if (Input.GetKey(KeyCode.S)) { Vector2 dest = rigidbody2.position + Vector2.down * moveSpeed / 10; rigidbody2.MovePosition(dest); animator.ChangeDir(SelfAnim.MoveDirAnim.Down); } else if (Input.GetKey(KeyCode.A)) { Vector2 dest = rigidbody2.position + Vector2.left * moveSpeed / 10; rigidbody2.MovePosition(dest); animator.ChangeDir(SelfAnim.MoveDirAnim.Left); } else if (Input.GetKey(KeyCode.D)) { Vector2 dest = rigidbody2.position + Vector2.right * moveSpeed / 10; rigidbody2.MovePosition(dest); animator.ChangeDir(SelfAnim.MoveDirAnim.Right); } } }
private void CalculateDir(Vector3 prev, Vector3 next) // the ghost move direction { float xx = next.x - prev.x; float yy = next.y - prev.y; if (Mathf.Abs(yy) > Mathf.Abs(xx)) { animator.ChangeDir(yy > 0 ? SelfAnim.Anim_Dir.Up : SelfAnim.Anim_Dir.Down); } else { animator.ChangeDir(xx > 0 ? SelfAnim.Anim_Dir.Right : SelfAnim.Anim_Dir.Left); } }
private void CalculateDir(Vector3 prev, Vector3 next) { float xx = next.x - prev.x; float yy = next.y - prev.y; if (Mathf.Abs(yy) > Mathf.Abs(xx)) { animator.ChangeDir(yy > 0 ? SelfAnim.MoveDirAnim.Up : SelfAnim.MoveDirAnim.Down); } else { animator.ChangeDir(yy > 0 ? SelfAnim.MoveDirAnim.Right : SelfAnim.MoveDirAnim.Left); } }