Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
Mob rotating towards playerSandra MoenI made it so the mob is rotating towards the player when chasing. I just had to add it, and it looks great : )
```gdscript
...
@export var rotation_acceleration := 5.0
func _physics_process(delta: float) -> void:
if !_player:
...
else:
...
var angle_to_player := global_position.angle_to_point(_player.global_position)
rotation = rotate_toward(rotation, angle_to_player, rotation_acceleration * delta)
move_and_slide()
```12Feb. 25, 2025
Mob sticking to playerChefGorgeousEagle100Hello! I have triple checked and my motion mode is definitely set to floating for both the mob and the player. But the mob is still sticking to the player. And once the mob reaches the player I can't move up anymore, seems like it's a collision shape issue maybe? Anywhere else I can look? Here's my code if that helps.
```gdscript
class_name Mob extends CharacterBody2D
var _player: Player = null
@onready var area_2d = $Area2D
@export var speed := 600.0
@onready var acceleration = 1200.0
func _ready():
area_2d.body_entered.connect(on_body_entered)
area_2d.body_exited.connect(on_body_exited)
func _physics_process(delta):
if _player == null:
velocity = velocity.move_toward(Vector2.ZERO, delta* acceleration)
else:
var direction := global_position.direction_to(_player.global_position)
var desired_velocity = direction * speed
velocity = velocity.move_toward(desired_velocity, delta * acceleration)
move_and_slide()
func on_body_entered(body):
if body is Player:
_player = body
func on_body_exited(body):
if body is Player:
_player = null
```
10Mar. 30, 2025
How do I repel the mob after a successful hit on the Player?ThirtyOlivesI was attempting to write a lambda function in the
```gdscript
_hitbox_area.body_entered.connect
```
to move the enemy away slightly after a hit but this was unsuccessful because the physics process would immediately reset the velocity
I was determining the direction to move in by multiplying global_position.direction_to(_player.global_position) by negative 1, would the Vector2 function bounce() be more suited for this?
Would writing code to repel enemy mobs be better achieved in the Player script? this would repel all but I'm not sure how to scriptwise interact with the necessary collision layers10Mar. 18, 2025
My solution came a little different, Am I going to have problems on future lessons?blind-ibisPlease let me know.
```gdscript
extends CharacterBody2D
@export var speed : float = 200
@export var brake_factor: float = 5
var is_player_close : bool = false
var direction: Vector2 = Vector2.ZERO
var player : Player
func _physics_process(delta: float) -> void:
if is_player_close == true:
direction = global_position.direction_to(player.global_position)
velocity = direction * speed
elif is_player_close == false:
velocity = velocity.move_toward(Vector2.ZERO, brake_factor)
move_and_slide()
func _on_area_2d_body_entered(body: Node2D) -> void:
if body is Player:
is_player_close = true
player = body
return
func _on_area_2d_body_exited(body: Node2D) -> void:
if body is Player:
is_player_close = false
player = null
return
```
30Mar. 11, 2025
Little note on reference code:PurpleHuesI noticed that when using % on DetectionArea I get an error "Invalid access to property or key 'body_entered' on a base object or type 'null_instance'. When I replace % with $ it works fine, but what does the error mean?30Feb. 20, 2025
My Sword mob Code!pikminfan71Hope it's ok!
```gdscript
class_name Mob
extends CharacterBody2D
@onready var _area_2d: Area2D = %Area2D
@onready var _player: Player = null
@export_range(200.0, 2000.0, 1) var speed: float = 800.0
func _physics_process(delta: float) -> void:
if _player == null:
var direction : Vector2 = Vector2.ZERO
velocity = velocity.move_toward(direction, speed / 2.0 * delta)
elif _player:
var direction = global_position.direction_to(_player.global_position)
var distance = global_position.distance_to(_player.global_position)
velocity = velocity.move_toward(direction * distance, speed * delta)
move_and_slide()
func _ready() -> void:
_area_2d.body_entered.connect(func(body: Node) -> void:
if body is Player:
_player = body
)
_area_2d.body_exited.connect(func(body: Node) -> void:
if body is Player:
_player = null
)
```50Feb. 04, 2025
Do we really need "if player = null"?oliver_gzzHey there, when I was comparing the code that I made based on past lessons, I ended up with this in my func `_physics_process(delta)` **code below**
my question is, do we really need the line:
`if _player == null:`
` velocity = velocity.move_toward(Vector2.ZERO, acceleration * delta)`
**this is the code I have:**
```gdscript
if _player != null:
var direction := global_position.direction_to(_player.global_position)
var distance := global_position.distance_to(_player.global_position)
var speed := max_speed if distance > 100 else max_speed / 200
var desired_velocity := direction * speed
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
move_and_slide()
```40Jan. 30, 2025
Question about global_positionkazaHello! I'm loving this course! But i get a question:
I achieve the final result of this chapter and i forgot and i used position instead global_position.
Do you can you explain me why position (instead global_position) can lead to unexpected results or bugs?20Jan. 29, 2025
My solutionPurpleSunriseHello!
My code was very similar to the reference! I didn't implement the feature that the swords slows down when it gets closer to the player. Do I have to modify the code to adapt it to the reference or is it ok if it's a bit different?
```gdscript
class_name Mob extends CharacterBody
@onready var detect_area: Area2D = %DetectArea
@export var max_speed : float = 200.0
@export var acceleration : float = 1000.0
@export var deceleration : float = 400.0
var _player : Player = null
func _ready() -> void:
detect_area.body_entered.connect(func(body : Node)->void:
if body is Player:
_player = body)
detect_area.body_exited.connect(func(body : Node)->void:
if body is Player:
_player = null)
func _physics_process(delta: float) -> void:
if _player != null:
var direction = global_position.direction_to(_player.global_position)
var desired_velocity : Vector2 = max_speed * direction
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
else:
velocity = velocity.move_toward(Vector2.ZERO, deceleration * delta)
move_and_slide()
```10Jan. 23, 2025
why i am getting this error?AngryCapybaraHello! Could you please help me?
I am defining the `player` variable exactly like this:
`var _player: Player = null`
However, I keep getting the following error:
`Parse Error: Could not find type "Player" in the current scope.`
What am I missing?30Jan. 09, 2025
Motion Mode: Grounded / FloatingRuben TeijeiroThere is some error when explaining the Motion Mode property of the CharacterBody2D.
The initial explanation in the Challenge is:
> Make sure to set the `CharacterBody2D` node's *Motion Mode* property to *Grounded*, otherwise, the mob will stick to the player when colliding with them.
But in the Questions section it's explained like this:
> The `CharacterBody2D` node has a property called *Motion Mode*. By default, it's set to *Grounded*, which is designed for games with a side view and tries to detect slopes, walls, and ceilings. You need to change it to *Floating* for top-down games.
I assume the second is the correct one, right?10Dec. 06, 2024
DecelerationMarcJLooking at the version of the code reference I don't see the velocity ever decelerating or reset when the player exits the mob detection range. So it seems to me that the acceleration will only occur the first time the player enters the mob range and then the next time the player enters into the detection range the mob moves at max speed or am I missing something?10Nov. 19, 2024
Rotation for the weaponiguessfiveI finding it difficult to identify where the rotation is set for the weapon to pass to the bullet.
Is the rotation for the weapon set based on the direction the player is facing or the angle of the mouse's global position if using mouse to shoot the bullet. Or either is fine.20Nov. 18, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.