diff --git a/tutorials/math/vectors_advanced.rst b/tutorials/math/vectors_advanced.rst index 594617a302e..9bf304f256a 100644 --- a/tutorials/math/vectors_advanced.rst +++ b/tutorials/math/vectors_advanced.rst @@ -178,7 +178,7 @@ degrees to either side: .. code-tab:: gdscript GDScript # Calculate vector from `a` to `b`. - var dvec = (point_b - point_a).normalized() + var dvec = point_a.direction_to(point_b) # Rotate 90 degrees. var normal = Vector2(dvec.y, -dvec.x) # Alternatively (depending the desired side of the normal): @@ -187,7 +187,7 @@ degrees to either side: .. code-tab:: csharp // Calculate vector from `a` to `b`. - var dvec = (pointB - pointA).Normalized(); + var dvec = pointA.DirectionTo(pointB); // Rotate 90 degrees. var normal = new Vector2(dvec.y, -dvec.x); // Alternatively (depending the desired side of the normal): diff --git a/tutorials/navigation/navigation_introduction_2d.rst b/tutorials/navigation/navigation_introduction_2d.rst index 1fb8066125c..4c63b213d1d 100644 --- a/tutorials/navigation/navigation_introduction_2d.rst +++ b/tutorials/navigation/navigation_introduction_2d.rst @@ -148,11 +148,7 @@ NavigationServer2D and a NavigationAgent2D for path movement. var current_agent_position: Vector2 = global_position var next_path_position: Vector2 = navigation_agent.get_next_path_position() - var new_velocity: Vector2 = next_path_position - current_agent_position - new_velocity = new_velocity.normalized() - new_velocity = new_velocity * movement_speed - - velocity = new_velocity + velocity = current_agent_position.direction_to(next_path_position) * movement_speed move_and_slide() .. code-tab:: csharp C# @@ -199,11 +195,7 @@ NavigationServer2D and a NavigationAgent2D for path movement. Vector2 currentAgentPosition = GlobalTransform.Origin; Vector2 nextPathPosition = _navigationAgent.GetNextPathPosition(); - Vector2 newVelocity = (nextPathPosition - currentAgentPosition).Normalized(); - newVelocity *= _movementSpeed; - - Velocity = newVelocity; - + Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed; MoveAndSlide(); } diff --git a/tutorials/navigation/navigation_introduction_3d.rst b/tutorials/navigation/navigation_introduction_3d.rst index 7b32bf5fe11..f29cbc13d78 100644 --- a/tutorials/navigation/navigation_introduction_3d.rst +++ b/tutorials/navigation/navigation_introduction_3d.rst @@ -153,11 +153,7 @@ a NavigationAgent3D for path movement. var current_agent_position: Vector3 = global_position var next_path_position: Vector3 = navigation_agent.get_next_path_position() - var new_velocity: Vector3 = next_path_position - current_agent_position - new_velocity = new_velocity.normalized() - new_velocity = new_velocity * movement_speed - - velocity = new_velocity + velocity = current_agent_position.direction_to(next_path_position) * movement_speed move_and_slide() .. code-tab:: csharp C# @@ -204,11 +200,7 @@ a NavigationAgent3D for path movement. Vector3 currentAgentPosition = GlobalTransform.Origin; Vector3 nextPathPosition = _navigationAgent.GetNextPathPosition(); - Vector3 newVelocity = (nextPathPosition - currentAgentPosition).Normalized(); - newVelocity *= _movementSpeed; - - Velocity = newVelocity; - + Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed; MoveAndSlide(); } diff --git a/tutorials/navigation/navigation_using_navigationagents.rst b/tutorials/navigation/navigation_using_navigationagents.rst index 6044e0fce73..2ad7109a313 100644 --- a/tutorials/navigation/navigation_using_navigationagents.rst +++ b/tutorials/navigation/navigation_using_navigationagents.rst @@ -202,8 +202,7 @@ This script adds basic navigation movement to a Node3D with a NavigationAgent3D movement_delta = movement_speed * delta var next_path_position: Vector3 = navigation_agent.get_next_path_position() - var current_agent_position: Vector3 = global_position - var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta + var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_delta if navigation_agent.avoidance_enabled: navigation_agent.set_velocity(new_velocity) else: @@ -236,8 +235,7 @@ This script adds basic navigation movement to a CharacterBody3D with a Navigatio return var next_path_position: Vector3 = navigation_agent.get_next_path_position() - var current_agent_position: Vector3 = global_position - var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed + var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed if navigation_agent.avoidance_enabled: navigation_agent.set_velocity(new_velocity) else: @@ -271,8 +269,7 @@ This script adds basic navigation movement to a RigidBody3D with a NavigationAge return var next_path_position: Vector3 = navigation_agent.get_next_path_position() - var current_agent_position: Vector3 = global_position - var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed + var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed if navigation_agent.avoidance_enabled: navigation_agent.set_velocity(new_velocity) else: diff --git a/tutorials/navigation/navigation_using_navigationpaths.rst b/tutorials/navigation/navigation_using_navigationpaths.rst index c141fe348c7..72114410443 100644 --- a/tutorials/navigation/navigation_using_navigationpaths.rst +++ b/tutorials/navigation/navigation_using_navigationpaths.rst @@ -121,6 +121,6 @@ the default navigation map by setting the target position with ``set_movement_ta current_path_point = current_path[current_path_index] - var new_velocity: Vector3 = (current_path_point - global_transform.origin).normalized() * movement_delta + var new_velocity: Vector3 = global_transform.origin.direction_to(current_path_point) * movement_delta global_transform.origin = global_transform.origin.move_toward(global_transform.origin + new_velocity, movement_delta) diff --git a/tutorials/physics/rigid_body.rst b/tutorials/physics/rigid_body.rst index 6f221812d67..2f72f983464 100644 --- a/tutorials/physics/rigid_body.rst +++ b/tutorials/physics/rigid_body.rst @@ -39,7 +39,7 @@ Here is a custom ``look_at()`` method that will work reliably with rigid bodies: func look_follow(state, current_transform, target_position): var up_dir = Vector3(0, 1, 0) var cur_dir = current_transform.basis * Vector3(0, 0, 1) - var target_dir = (target_position - current_transform.origin).normalized() + var target_dir = current_transform.origin.direction_to(target_position) var rotation_angle = acos(cur_dir.x) - acos(target_dir.x) state.angular_velocity = up_dir * (rotation_angle / state.step) @@ -58,7 +58,7 @@ Here is a custom ``look_at()`` method that will work reliably with rigid bodies: { var upDir = new Vector3(0, 1, 0); var curDir = currentTransform.Basis * new Vector3(0, 0, 1); - var targetDir = (targetPosition - currentTransform.Origin).Normalized(); + var targetDir = currentTransform.Origin.DirectionTo(targetPosition); var rotationAngle = Mathf.Acos(curDir.X) - Mathf.Acos(targetDir.X); state.SetAngularVelocity(upDir * (rotationAngle / state.GetStep()));