need help with acceleration and declaration

4.4 Godot Version

Question

so im trying to make a 3d sonic game and i like how i got acceleration to work but my declaration code dosint work wright here’s everything because i don’t know what to show and not to

extends CharacterBody3D

var MAXSPEED = 15.0
var SPEEDCAP = 20.0
var JUMP_VELOCITY = 6.0
var DECEL = 0.3
var ACCEL = 0.08
var SPEED = -1
var xform : Transform3D



func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("camramove_left"):
		$camera_controller.rotate_y(deg_to_rad(10))
	if Input.is_action_just_pressed("camramove_right"):
		$camera_controller.rotate_y(deg_to_rad(-10))
	
	
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY - DECEL 
		

	# 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("move_left", "move_right", "move_froward", "move_backward")
	
	var direction = ($camera_controller.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	 
	if input_dir != Vector2(0,0):
		$MeshInstance3D.rotation_degrees.y = $camera_controller.rotation_degrees.y - rad_to_deg(input_dir.angle()) + 90
	
	if is_on_floor():
		align_with_floor($RayCast3D.get_collision_normal())
		global_transform = xform
	
	if direction:
		# Speed up acceleration
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
		if SPEED < MAXSPEED:
			SPEED = ACCEL+SPEED
		print("SPEED:",SPEED)
	elif SPEED > 0:
		# Slow down
		SPEED = SPEED - DECEL 
		velocity.x = move_toward(velocity.x, SPEED, 0)
		velocity.z = move_toward(velocity.z, SPEED, 0)
		print("SPEED:",SPEED)
	else:
		# Stop
		SPEED = 0
		velocity.x = move_toward(velocity.x, 0, MAXSPEED)
		velocity.z = move_toward(velocity.z, 0, MAXSPEED) 
		print("SPEED:",SPEED)
	move_and_slide()
	
	
	
	$camera_controller.position = lerp($camera_controller.position, position ,0.08)
	
	
func align_with_floor(floor_normal):
	xform = global_transform
	xform.basis.y = floor_normal
	xform.basis.x = -xform.basis.z.cross(floor_normal)
	xform.basis = xform.basis.orthonormalized()

basically when i stop pressing a direction the character slides and then stops, is there a way for him to visually slow down like how the acceleration works because i cant find i way to do that(side note if some one knows how to add physics that would be nice(like if i go fast on a ramp and jump i get hight ect. stuff like that))

You should perhaps start with something simpler than Sonic.

well i did,i got some stuff down its just the movement physics are hard to code(for me)

It would probably be better to find a more consistent way of handling acceleration and deacceleration, but I guess something like this should work:

	elif SPEED > 0:
		# Slow down
		var new_speed = max(0.0, SPEED - DECEL)
		velocity.x = lerp(0.0, velocity.x, new_speed / SPEED)
		velocity.z = lerp(0.0, velocity.z, new_speed / SPEED)
		SPEED = new_speed
		print("SPEED:",SPEED)

Furthermore, when accelerating, you should probably modify SPEED first and set the velocity afterwards. Otherwise your effective speed is always one physics tick behind.

1 Like

Thanks!

OH and this helped me get air declaration THANK YOU