How do I get friction to apply to the child object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godotuser5675

Sword code:

extends Area2D

export var velocity_initial = 0
export var velocity_initial_max = 500
export var velocity_current_max = 500
export var velocity_current = Vector2()
var once = true
var sword_max_clockwise = PI/2
var sword_max_counter_clockwise = PI/2
const sword_clockwise_buffer = PI/6000000
const right_vector = Vector2()
var area_ignore_bodies = ["Player", "Enemy"]
var area_ignore_areas = []
var harm_group = ["Enemy"]
var enemy_body_touched = []
var deactivate_area_bodies = []
var deactivate_area_areas = []

var inactive = false


onready var velocity = {
	"Initial" : velocity_initial,
	"Initial Max" : velocity_initial_max,
	"Current Max" : velocity_current_max,
	"Current" : velocity_current
	}

export var rotation_velocity_initial = 0
export var rotation_velocity_initial_max = PI * 10
export var rotation_velocity_current_max = PI * 10
export var rotation_velocity_current = 0

onready var rotation_velocity = {
	"Initial" : rotation_velocity_initial,
	"Initial Max" : rotation_velocity_initial_max,
	"Current Max" : rotation_velocity_current_max,
	"Current" : rotation_velocity_current
	}

export var rotation_acceleration_initial_max_length = PI * 10
export var rotation_acceleration_max_length = PI * 10
export var rotation_acceleration_current = 0

onready var rotation_acceleration = {
	"Initial Max Length" : rotation_acceleration_initial_max_length,
	"Max Length" : rotation_acceleration_max_length,
	"Current" : rotation_acceleration_current
	}


export var acceleration_initial_max_length = 1000
export var acceleration_max_length = 1000
export var acceleration_current = Vector2()

onready var acceleration = {
	"Initial Max Length" : acceleration_initial_max_length,
	"Max Length" : acceleration_max_length,
	"Current" : acceleration_current
	}

export var friction_max_length = 400
export var friction_max_stop_length = 2000

onready var friction = {
	"Max Length" : friction_max_length,
	"Max Stop Length" : friction_max_stop_length
	
	
	
	}
	
	
export var rotation_friction_max_length = PI/2
export var rotation_friction_max_stop_length = PI * 5

onready var rotation_friction = {
	"Max Length" : rotation_friction_max_length,
	"Max Stop Length" : rotation_friction_max_stop_length
	
	
	
	}

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	pass
func _process(delta):
	
	for N in enemy_body_touched:
		for B in harm_group:
			if N != null:
				if N.is_in_group(B) and !inactive:
					enemy_body_touched.erase(N)
					N.damage()
					
	pass



func handle_velocity(delta, move_h, move_v):
	
	acceleration["Current"] = Vector2()
	acceleration["Current"] += Vector2(move_h, move_v) * acceleration["Max Length"]

#
#
	#Adds acceleration to velocity
	if(acceleration["Current"].length() != 0):
		velocity["Current"] += acceleration["Current"] * delta
		if(velocity["Current"].length() >= friction["Max Length"] * delta):
			velocity["Current"] += right_vector.rotated(velocity["Current"].angle() + PI) * friction["Max Length"] * delta
		else:
			velocity["Current"] = Vector2()


	elif (velocity["Current"].length() >= friction["Max Stop Length"] * delta):
		velocity["Current"] += right_vector.rotated(velocity["Current"].angle() + PI) * friction["Max Stop Length"] * delta
	else:
		velocity["Current"] = Vector2()
#
	
	
#	if(velocity["Current"].length() > velocity["Current Max"]):
#		velocity["Current"] = right_vector.rotated(velocity["Current"].angle()) * velocity["Current Max"]
#
#	velocity["Current"] += right_vector.rotated(velocity["Current"].angle() + PI) * (acceleration["Max Length"] - friction["Max Length"]) * ((velocity["Current"].length()/velocity["Current Max"]) * (acceleration["Current"]/acceleration["Max Length"])) * delta
	

	
	position += velocity["Current"] * delta
	


func handle_rotation(delta, clockwise):

	rotation_acceleration["Current"] = 0

	rotation_acceleration["Current"] += clockwise * rotation_acceleration["Max Length"]
	if(rotation > sword_max_clockwise + sword_clockwise_buffer):
		rotation = sword_max_clockwise
		rotation_acceleration["Current"] = 0
		rotation_velocity["Current"] = 0
	elif(rotation < -sword_max_clockwise - sword_clockwise_buffer):
		rotation = -sword_max_clockwise
		rotation_acceleration["Current"] = 0
		rotation_velocity["Current"] = 0
	
	

		
	if(rotation_acceleration["Current"] * delta != 0):
		rotation_velocity["Current"] += rotation_acceleration["Current"] * delta
		if(abs(rotation_velocity["Current"]) >= abs(rotation_friction["Max Length"]) * delta):
			rotation_velocity["Current"] += -sign(rotation_velocity["Current"]) * rotation_friction["Max Length"] * delta
		else:
			rotation_velocity["Current"] = 0



	elif(abs(rotation_velocity["Current"]) >= abs(rotation_friction["Max Stop Length"]) * delta):
		rotation_velocity["Current"] += -sign(rotation_velocity["Current"]) * rotation_friction["Max Stop Length"] * delta
	else:
			rotation_velocity["Current"] = 0
	
	
	
	
	#Fix later

	rotation += rotation_velocity["Current"] * delta
	rotation_velocity["Current"] += -sign(rotation_velocity["Current"]) * (rotation_acceleration["Max Length"] - rotation_friction["Max Length"]) * (abs(rotation_velocity["Current"])/rotation_velocity["Current Max"]) * delta
	
	var col = null
	

	return [col, abs(rotation_acceleration["Current"]/rotation_acceleration["Max Length"])]

Relevant Entity code:

func _physics_process(delta):
	get_input(delta)
	stamina.regenerate(delta)
	move(delta)
	var col = [false]
	if has_node("Sword"):
		$Sword.handle_velocity(delta, input.sword_move_h , input.sword_move_v)
		if(stamina.left()):
			col = $Sword.handle_rotation(delta, input.sword_clockwise)
			stamina.deplete(col[1] * sword_rotate["Current"] * stamina.remove * delta)
		else:
			$Sword.handle_rotation(delta, 0)

What Happens is that the sword moves when I enter in the input but after I let go of the right stick it keeps the current velocity and doesn’t stop.

Player Code:

extends "res://scripts/Entity.gd"





func _ready():
	add_to_group("Player")
	$Sword.harm_group = ["Enemy"]
	$Sword.area_ignore_bodies = ["Player", "Enemy"]
	pass

func get_input(delta):
	input.move_h =  Input.get_joy_axis(0,0)
	input.move_v =  Input.get_joy_axis(0,1)
	input.clockwise =  int(Input.is_action_pressed("clockwise")) - int(Input.is_action_pressed("counter clockwise"))
	input.sword_clockwise = Input.get_joy_axis(0,7) - Input.get_joy_axis(0,6) 
	input.sprint = Input.is_action_pressed("sprint")
	input.teleport = Input.is_action_just_pressed("teleport")
	input.teleport_position = get_global_mouse_position()
	input.sword_move_h = Input.get_joy_axis(0,2)
	input.sword_move_v = Input.get_joy_axis(0,3)

	if(abs(input.move_h) < dead_zone and abs(input.move_v) < dead_zone):
		input.move_h = 0
		input.move_v = 0
	if(abs(input.sword_move_h) < dead_zone +0.2  and abs(input.sword_move_v) < dead_zone +0.2):
		input.sword_move_h = 0
		input.sword_move_v = 0

$Sword is a child of $Entity

Godotuser5675 | 2018-09-06 22:23

:bust_in_silhouette: Reply From: Godotuser5675

I solved the problem.

Here is code in Entity:

func handle_velocity(delta, vel, accel, fric,  i_move_h, i_move_v, obj, st,  r_vec= Vector2(1, 0)):
	vel["Current"] += r_vec.rotated(vel["Current"].angle() + PI) * (accel["Max Length"] - fric["Max Length"]) * ((vel["Current"].length()/vel["Current Max"])) * delta
	accel["Current"] = Vector2()
	accel["Current"] += Vector2(i_move_h, i_move_v) * accel["Max Length"]

	if(!$Stamina.depletion_system($Stamina.remove * move["Current"] * delta * (accel["Current"].length()/accel["Max Length"]))):
		accel["Current"] = Vector2()
	
	#Adds accel to vel
	if(accel["Current"].length() != 0):
		vel["Current"] += accel["Current"] * delta
		if(vel["Current"].length() >= fric["Max Length"] * delta):
			vel["Current"] += r_vec.rotated(vel["Current"].angle() + PI) * fric["Max Length"] * delta
		else:
			vel["Current"] = Vector2()
		
		
	elif (vel["Current"].length() >= fric["Max Stop Length"] * delta):
		vel["Current"] += r_vec.rotated(vel["Current"].angle() + PI) * fric["Max Stop Length"] * delta
	else:
		vel["Current"] = Vector2()
		
	
	

	if(obj.is_class("KinematicBody2D")):
		var pre_pos = obj.position
		obj.move_and_slide(vel["Current"])
		var cur_vel = obj.position - pre_pos
		vel["Current"] = cur_vel/delta
	else:
		obj.position += vel["Current"] * delta

And this line handles the swords’s velocity:

	handle_velocity(delta, $Sword.velocity, $Sword.acceleration, $Sword.friction, $input.sword_move_h, $input.sword_move_v, $Sword, $Stamina)