void FixedUpdate() { if (TheGame.Get().IsPaused()) { rigid.velocity = Vector3.zero; return; } if (IsDead()) { return; } PlayerControls controls = PlayerControls.Get(player_id); PlayerControlsMouse mcontrols = PlayerControlsMouse.Get(); Vector3 tmove = Vector3.zero; //Update auto move for moving targets GameObject auto_move_obj = null; if (auto_move_select != null && auto_move_select.type == SelectableType.Interact) { auto_move_obj = auto_move_select.gameObject; } if (auto_move_attack != null) { auto_move_obj = auto_move_attack.gameObject; } if (auto_move && auto_move_obj != null) { Vector3 diff = auto_move_obj.transform.position - auto_move_target; if (diff.magnitude > 1f) { auto_move_target = auto_move_obj.transform.position; auto_move_target_next = auto_move_obj.transform.position; CalculateNavmesh(); //Recalculate navmesh because target moved } } //Navmesh calculate next path if (auto_move && use_navmesh && path_found && path_index < nav_paths.Length) { auto_move_target_next = nav_paths[path_index]; Vector3 move_dir_total = auto_move_target_next - transform.position; move_dir_total.y = 0f; if (move_dir_total.magnitude < 0.2f) { path_index++; } } //AUTO Moving (after mouse click) auto_move_timer += Time.fixedDeltaTime; if (auto_move && auto_move_timer > 0.02f) //auto_move_timer to let the navmesh time to calculate a path { Vector3 move_dir_total = auto_move_target - transform.position; Vector3 move_dir_next = auto_move_target_next - transform.position; Vector3 move_dir = move_dir_next.normalized * Mathf.Min(move_dir_total.magnitude, 1f); move_dir.y = 0f; float move_dist = Mathf.Min(GetMoveSpeed(), move_dir.magnitude * 10f); tmove = move_dir.normalized * move_dist; } //Keyboard/gamepad moving else if (IsControlsEnabled()) { Vector3 cam_move = TheCamera.Get().GetRotation() * controls.GetMove(); if (mcontrols.IsJoystickActive() && !character_craft.IsBuildMode()) { Vector2 joystick = mcontrols.GetJoystickDir(); cam_move = TheCamera.Get().GetRotation() * new Vector3(joystick.x, 0f, joystick.y); } tmove = cam_move * GetMoveSpeed(); } //CancelAction if (is_action && can_cancel_action && tmove.magnitude > 0.1f) { CancelAction(); } //Stop moving if doing action if (is_action) { tmove = Vector3.zero; } //Check if grounded DetectGrounded(); //Add Falling to the move vector if (!is_grounded || IsJumping()) { if (!IsJumping()) { fall_vect = Vector3.MoveTowards(fall_vect, Vector3.down * fall_speed, fall_gravity * Time.fixedDeltaTime); } tmove += fall_vect; } //Add slope angle else if (is_grounded) { tmove = Vector3.ProjectOnPlane(tmove.normalized, ground_normal).normalized *tmove.magnitude; } //Apply the move calculated previously move = Vector3.Lerp(move, tmove, move_accel * Time.fixedDeltaTime); rigid.velocity = move; //Calculate Facing if (!is_action && IsMoving()) { facing = new Vector3(move.x, 0f, move.z).normalized; } //Rotate character with right joystick when not in free rotate mode bool freerotate = TheCamera.Get().IsFreeRotation(); if (!is_action && !freerotate && controls.IsGamePad()) { Vector2 look = controls.GetFreelook(); Vector3 look3 = TheCamera.Get().GetRotation() * new Vector3(look.x, 0f, look.y); if (look3.magnitude > 0.5f) { facing = look3.normalized; } } //Apply the facing Quaternion targ_rot = Quaternion.LookRotation(facing, Vector3.up); rigid.MoveRotation(Quaternion.RotateTowards(rigid.rotation, targ_rot, rotate_speed * Time.fixedDeltaTime)); //Fronted (need to be done after facing to avoid issues) DetectFronted(); //Check the average traveled movement (allow to check if character is stuck) Vector3 last_frame_travel = transform.position - prev_pos; move_average = Vector3.MoveTowards(move_average, last_frame_travel, 1f * Time.fixedDeltaTime); prev_pos = transform.position; //Stop auto move bool stuck_somewhere = move_average.magnitude <0.02f && auto_move_timer> 1f; if (stuck_somewhere) { auto_move = false; } //Stop the click auto move when moving with keyboard/joystick/gamepad if (controls.IsMoving() || mcontrols.IsJoystickActive() || mcontrols.IsDoubleTouch()) { StopAutoMove(); } }