GDCustomRaycastVehicle icon indicating copy to clipboard operation
GDCustomRaycastVehicle copied to clipboard

Input being handled more than once (4WD)

Open ghost opened this issue 2 years ago • 0 comments

Input is being accumulated for each ray in rayElements. 4WheelRayVehicleController.gd

func handle4WheelDrive(delta) -> void:
	# 4WD with front wheel steering
	for ray in rayElements:
		# get throttle axis
		var throttle : float = Input.get_axis("ui_down", "ui_up")
		# get steering axis
		var steering : float = Input.get_axis("ui_left", "ui_right")
		
		# steer wheels gradualy based on steering input
		if steering != 0:
			currentSteerAngle -= steering * steerSpeed * delta
		else:
			# return wheels to center
			if !is_equal_approx(currentSteerAngle, 0.0):
				if currentSteerAngle > 0:
					currentSteerAngle -= wheelReturnSpeed * delta
				else:
					currentSteerAngle += wheelReturnSpeed * delta
		currentSteerAngle = clamp(currentSteerAngle, -steeringAngle, steeringAngle)
		frontRightWheel.rotation_degrees.y = currentSteerAngle
		frontLeftWheel.rotation_degrees.y = currentSteerAngle
		
		# apply drive force
		if throttle != 0:
			currentDrivePower = lerp(currentDrivePower, throttle * drivePerRay, delta * acceleration)
		else:
			currentDrivePower = lerp(currentDrivePower, 0.0 , delta * acceleration * 1.5)
		ray.applyDriveForce(global_transform.basis.z * currentDrivePower)

Currently, steerSpeed, wheelReturnSpeed, and acceleration are being adjusted once per wheel (4 times on the VBL). Removing it from the for loop and multiplying the affected variables by 4 should keep vehicle handling identical. For Example:

export(float) var steerSpeed : float = 40.0
export(float) var wheelReturnSpeed : float = 120.0
export(float) var acceleration : float = 1.0
func handle4WheelDrive(delta) -> void:
	# 4WD with front wheel steering
	# get throttle axis
	var throttle : float = Input.get_axis("ui_down", "ui_up")
	# get steering axis
	var steering : float = Input.get_axis("ui_left", "ui_right")
	
	# steer wheels gradualy based on steering input
	if steering != 0:
		currentSteerAngle -= steering * steerSpeed * delta
	else:
		# return wheels to center
		if !is_equal_approx(currentSteerAngle, 0.0):
			if currentSteerAngle > 0:
				currentSteerAngle -= wheelReturnSpeed * delta
			else:
				currentSteerAngle += wheelReturnSpeed * delta
	currentSteerAngle = clamp(currentSteerAngle, -steeringAngle, steeringAngle)
	frontRightWheel.rotation_degrees.y = currentSteerAngle
	frontLeftWheel.rotation_degrees.y = currentSteerAngle
	
	# apply drive force
	if throttle != 0:
		currentDrivePower = lerp(currentDrivePower, throttle * drivePerRay, delta * acceleration)
	else:
		currentDrivePower = lerp(currentDrivePower, 0.0 , delta * acceleration * 1.5)
	for ray in rayElements:
		ray.applyDriveForce(global_transform.basis.z * currentDrivePower)

No real impact on performance, but looks cleaner.

ghost avatar May 27 '22 11:05 ghost