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); }
// Each physics step.. void FixedUpdate() { // Set some local float variables equal to the value of our Horizontal and Vertical Inputs float moveHorizontal = InputBroker.GetAxis("Horizontal"); float moveVertical = InputBroker.GetAxis("Vertical"); // Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); // Add a physical force to our Player rigidbody using our 'movement' Vector3 above, // multiplying it by 'speed' - our public player speed that appears in the inspector rb.AddForce(movement * speed); }