I’m currently refactoring the movement script for my 3D dungeon-crawler prototype, which previously used raycasts to detect collisions. I now want to utilize Godot’s CharacterBody3D collision functions instead, such as move_and_slide().
The issue is that I have been relying on tweens to move the player step by step, and now I’m unsure of how to utilize the collision functions I’d like.
For example, here is part of my current code for moving forward:
func _physics_process(_delta: float) -> void:
if player_controller.tween is Tween:
if player_controller.tween.is_running():
return
if Input.is_action_pressed("forward"):
player_controller.tween = create_tween().set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_IN_OUT)
player_controller.tween.tween_property(player_controller, "transform", player_controller.transform.translated_local(Vector3.FORWARD * 2), player_controller.SPEED)
I’d just like some advice regarding whether this is even desirable, or if I should be using something else instead of tweens for movement.
If your movement is grid based, you shouldn’t be using neither raycasts nor other physics collisions. Instead, maintain an occupancy data structure and use that to check if you can move to a wanted cell in the grid.
You can use move_and_slide or tweens, but not both. Using Godot’s collision can be easier than setting up your own grid-collision system, but you will be fighting it for some quirks.
This thread covers converting from tween-grids to velocity towards a target position if you choose to go down this route.
The main question is - is your movement completely grid based. If yes, then simply decouple the actual state of your grid from the visual representation (including colliders). The whole point of grid based movement is to optimize away any need for physics collisions that you’d normally use for an action game and rely on the logical state of the cells instead.
Some time ago I made a little demo for a user on another forum. They were in a very similar situation, trying to use colliders, rays etc for a system that’s in its essence completely grid based.
The demo builds a custom grid occupancy map from what it finds in a GridMap node. It even handles elevation, “bridges” and “npcs” as you can see in the gif. The movement in the grid is instant but its visual representation is smoothed with tweens. There’s a switch that lets you toggle smooth movement.
It’s 90 lines of code in total. Godot 4.3 I think.
Not sure if the whole thing will be directly usable for your case but you might find insightful seeing how this can be approached.