Not inhereting platform velocity

Godot Version

4.1.3 →

Question

Not sure what I’m doing wrong. But I thought the move_and_slide method applied the platform velocity to the character 3d node, so that when a player jumps off a moving platform it keeps moving in that direction, much like in real life. I’m new and struggling to make this work, but I seem to only be able to get it to work if I do not adjust the velocity at all.

Here’s my code:

@onready var anim_tree = $AnimationTree

var speed = 3
const DEFAULT_SPEED = 3
const SPRINT_VELOCITY = 6
const JUMP_VELOCITY = 3
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
const LERP_VAL = .15

func _ready():
	pass

func _physics_process(delta):
	
	if is_on_floor():
		if Input.is_action_just_pressed("Jump"):
			velocity.y += JUMP_VELOCITY
	else:
		velocity.y -= gravity * delta
		
	handle_movement()

func _process(delta):
	$TwistPivot.handle_camera_movement()

func handle_movement():
	var input = Input.get_vector("MoveLeft", "MoveRight", "MoveForward", "MoveBackward")
	var rotated_input =  Vector3(input.x, 0, input.y).rotated(Vector3.UP, $TwistPivot.rotation.y + deg_to_rad(45)) 
	var direction = transform.basis * rotated_input.normalized()

	
	if Input.is_action_pressed("Run"):
		speed = SPRINT_VELOCITY
	else:
		speed = DEFAULT_SPEED

	if direction:
		if is_on_floor():
			velocity.x = lerp(velocity.x, direction.x * speed, LERP_VAL)
			velocity.z = lerp(velocity.z, direction.z * speed, LERP_VAL)

		else:
			velocity.x = lerp(velocity.x, direction.x * speed, LERP_VAL)
			velocity.z = lerp(velocity.z, direction.z * speed, LERP_VAL)
			

	else:
		if is_on_floor():
			velocity.x = lerp(velocity.x, 0.0 * speed, LERP_VAL)
			velocity.z = lerp(velocity.z, 0.0 * speed, LERP_VAL)
			
	move_and_slide()

please help :smiling_face_with_tear:

It is usually better if you share your project. This way we can also find possible problems with nodes / in scenes and other project settings.
Compress it with Zip and upload it to Google Drive, for example, or create a Github repository (Github Desktop is very user-friendly if you don’t know it yet).

1 Like

Oh, does this mean my script actually checks out? I didn’t mention anything about my scene structure and property settings since it felt somewhat irrelevant. From my understanding, move_and _slide() takes the floor velocity into account, so what type of node and how it’s parented shouldn’t matter… my property settings are set to add velocity on leave, so you’d think that would do the trick, but I’ll take another look…

What’s odd is that I can get it to work when I do not attempt to adjust the velocity. So if my player has no directional input, it can jump and it’ll inherit the platform velocity just with the move_and_slide() function.

What is the platform made of?

It’s a CharacterBody3D.

If you don’t properly setup platform_floor_layers on your CharacyerBody3D then platform velocity will be ignored.

One thing you could try is changing the platform to a RigidBody

Yep, that was it. Thank you. Apparently it’s very difficult to inherent the platform velocity off of a character body.

Use an AnimatableBody3D for the platform and set your CharacterBody3D.platform_on_leave property to your desired behavior.

2 Likes

I must have imagined the solution. Going back it doesn’t seem to be working… neither with a rigid body or characterbody 3d. Inheriting the platform velocity seems impossible in godot.

Someone mentioned posting your project to GitHub, that’s probably the best idea right now then