Collison and code

Godot_v4.3-stable_win64###

Title: W, S, A, D Movement, Collision, and Preventing Passing Through Objects Issue

Hello Godot Community,

I’ve been working on a game using Godot 4.x, and I’m trying to implement a movement system with the following features:

W key to move upward (jump)
S key to move downward (descend)
A key to rotate around the character’s own axis to the right
D key to rotate around the character’s own axis to the left
In the process, I need the collision system to work correctly to prevent the character from passing through objects. Normally, when I apply gravity to objects, they don’t pass through each other, but with the code I’ve written, the character goes straight through objects when trying to rotate.

extends Node3D

Dönme ve hareket hızlarını ayarlayın

var rotation_speed = 2.0
var movement_speed = 5.0

func _ready():
# Başlangıçta bir şey yapmamıza gerek yok
pass

func _process(delta):
# move_up aksiyonuna basıldığında yukarı hareket
if Input.is_action_pressed(“move_up”): # ‘W’ tuşu
position.y += movement_speed * delta

# move_down aksiyonuna basıldığında aşağı hareket
if Input.is_action_pressed("move_down"):  # 'S' tuşu
	position.y -= movement_speed * delta

# move_left aksiyonuna basıldığında sola dönme
if Input.is_action_pressed("move_left"):  # 'A' tuşu
	rotate_y(-rotation_speed * delta)

# move_right aksiyonuna basıldığında sağa dönme
if Input.is_action_pressed("move_right"):  # 'D' tuşu
	rotate_y(rotation_speed * delta)

extends Node3D

const MOVE_LEFT = “move_left”
const MOVE_RIGHT = “move_right”
const MOVE_UP = “move_up”
const MOVE_DOWN = “move_down”

var speed = 5.0

func _physics_process(delta):
var direction = Vector3.ZERO

if Input.is_action_pressed(MOVE_UP):
    direction.y += speed
if Input.is_action_pressed(MOVE_DOWN):
    direction.y -= speed
if Input.is_action_pressed(MOVE_LEFT):
    rotate(Vector3(0, 1, 0), speed * delta)  # Y ekseninde dönüş
if Input.is_action_pressed(MOVE_RIGHT):
    rotate(Vector3(0, 1, 0), -speed * delta)  # Y ekseninde dönüş

# Hareketi position ile yapıyoruz
position += direction * delta

Use a CharacterBody3D, then the template script will be very similar to your own while using the Godot collision system.

Without a CharacterBody3D or RigidBody3D you will be on your own for collision detection, so best to use the former.

Don’t move your objects via the position or global_position directly, as collision detection won’t be used.

If extending CharacterBody3D, use the move_and_* functions.

If extending RigidBody3D, use its impulse methods.

extends CharacterBody3D

@export var SPEED = 6
@export var JUMP_VELOCITY = 4.5

func _ready():
pass # No unnecessary initial actions are performed

func _physics_process(delta):
# W key to move upward (Jump)
if Input.is_action_just_pressed(“move_up”) and is_on_floor():
velocity.y = JUMP_VELOCITY # Move upward (jump)

# S key to move downward (Descend)
if Input.is_action_just_pressed("move_down") and is_on_floor():
    velocity.y = -JUMP_VELOCITY  # Move downward (descend)

# A and D keys to rotate left and right
if Input.is_action_pressed("move_left"):  # A key
    rotation.y += deg_to_rad(SPEED * delta)  # Rotate around own axis to the right
if Input.is_action_pressed("move_right"):  # D key
    rotation.y -= deg_to_rad(SPEED * delta)  # Rotate around own axis to the left

# Direction of movement (W and S keys for upward and downward movement)
var input_dir = Vector3.ZERO
if Input.is_action_pressed("move_up"):  # W key
    input_dir.y = 1  # Move upward
if Input.is_action_pressed("move_down"):  # S key
    input_dir.y = -1  # Move downward

# Determine direction and apply movement
var direction = (transform.basis * input_dir).normalized()

if direction:
    velocity.x = direction.x * SPEED
    velocity.z = direction.z * SPEED
else:
    velocity.x = move_toward(velocity.x, 0, SPEED)
    velocity.z = move_toward(velocity.z, 0, SPEED)

# Move the character
move_and_slide()  # Collision detection is handled here, no need for Vector3.UP

In the above code, rotation works correctly with A and D keys, but up and down movement doesn’t work with W and S keys. I learned to use move_and_slide(velocity) function without parameters and it solved the problem, but up and down movement still doesn’t work. I’m looking for a solution that can help solve this problem. If possible, write the code yourself, I’m going crazy

Thanks!

Your up/down movement always checks is_on_floor(), but I have a feeling your player is not on the floor, try removing this and is_on_floor() check.

Also be sure to format your code pastes

1 Like

@gertkeno That’s my question - is this an actual jump or just up and down movement in a 3D space? Using the variable JUMP_VELOCITY might be confusing the issue.

@darkali630 I created several 360 degree movement options for my 3D helicopter game. Some of them were challenging but I learnt a lot by doing them.