godotengine / godotengine/godot

RayCast3D movement inside CharacterBody3D if calling CharacterBody3D movement in another script

Open
#101,372 3 comments 0 reactions 0 assignees View on GitHub
bug confirmed topic:3d topic:physics
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

Godot v4.3.stable.official [77dcf97d8]

### System information

Godot v4.3.stable - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4070 Ti SUPER (NVIDIA; 32.0.15.6614) - AMD Ryzen 7 7700 8-Core Processor (16 Threads)

### Issue description

A typical player controller looks like this (RayCast3D doesn't move in it):

https://github.com/user-attachments/assets/b029766d-8dc5-40b9-a8e0-1ee012191993

The player controller in which RayCast3D moves. The player movement is called not in the player script, but in another element and from another script file using a reference to the player controller. In this case, RayCast3D and ShapeCast3D move.

https://github.com/user-attachments/assets/c3de6c21-8fb1-45d4-b443-3c741f0dec1d

This bug is reproduced only if you call the function for player movement not in the player script, but from another script.

### Steps to reproduce

Good player conroller:

![Image](https://github.com/user-attachments/assets/79b96a04-12a9-4c22-b6fa-1ac2811207e6)

**player_good.gd**

```
extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

func _physics_process(delta: float) -> void:
move(delta)

func move(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).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_and_slide()

```

Bad player controller

![Image](https://github.com/user-attachments/assets/e0bd4fba-d1e4-4714-b3a1-bc6e273ade4b)

**player_bad.gd**

```

extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

func _physics_process(delta: float) -> void:
move(delta)

func move(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).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_and_slide()

```

**state_controller.gd**

```

extends Node

@onready var player_bad: CharacterBody3D = $"../PlayerBad"

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
player_bad.move(delta)
```

### Minimal reproduction project (MRP)

[bug_move_cas.zip](https://github.com/user-attachments/files/18368041/bug_move_cas.zip)

Contributor guide

Open the contributing guide

Research direction

Start by running the linked bug_move_cas.zip minimal reproduction in Godot v4.3 and compare player_good.gd, player_bad.gd, and state_controller.gd. Trace how CharacterBody3D movement is invoked from the external script and verify the RayCast3D and ShapeCast3D behavior in both cases. Done means the reproduced discrepancy is explained and the issue has a confirmed engine-side resolution or documented non-bug cause.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.