private void updatePosition() { Vector2 currentPos = new Vector2(transform.position.x, transform.position.y); if (!isMoving) { sendInput(); float horizontalInput = inputBroker.GetAxis("Horizontal"); float verticalInput = inputBroker.GetAxis("Vertical"); Vector2 inputVector = new Vector2(horizontalInput, verticalInput) * Gameboard.worldToGridRatio; inputVector = Vector2.ClampMagnitude(inputVector, Gameboard.worldToGridRatio); var destination = currentPos + inputVector; if (Gameboard.ValidateByWorldPos(destination)) { Gameboard.VacateByWorldPos(currentPos); target = currentPos + inputVector; Gameboard.OccupyByWorldPos(target); cardinalRenderer.SetDirection(target); } } float step = movementSpeed * Time.fixedDeltaTime; if (currentPos != target) { isMoving = true; // Debug.Log($"now moving to target {target} from {transform.position}"); transform.position = Vector2.MoveTowards(transform.position, target, step); } else { isMoving = false; } }
void FixedUpdate() { this.sendInput(); Vector2 currentPos = rbody.position; float horizontalInput = inputBroker.GetAxis("Horizontal"); float verticalInput = inputBroker.GetAxis("Vertical"); Vector2 inputVector = new Vector2(horizontalInput, verticalInput); inputVector = Vector2.ClampMagnitude(inputVector, 1); Vector2 movement = inputVector * movementSpeed; Vector2 newPos = currentPos + movement * Time.fixedDeltaTime; renderer.SetDirection(movement); rbody.MovePosition(newPos); }