diff options
Diffstat (limited to 'godot/scripts/characters/player_controller_new.gd')
| -rw-r--r-- | godot/scripts/characters/player_controller_new.gd | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/godot/scripts/characters/player_controller_new.gd b/godot/scripts/characters/player_controller_new.gd index 6aec2d9..6df88f0 100644 --- a/godot/scripts/characters/player_controller_new.gd +++ b/godot/scripts/characters/player_controller_new.gd @@ -19,6 +19,7 @@ var direction := Vector3() var move_axis := Vector2() var floorspeed := Vector3() var jumping = false +var can_jump = true onready var nav = $NavigationAgent # Walk @@ -26,22 +27,21 @@ const FLOOR_MAX_ANGLE: float = deg2rad(46.0) export(float) var jump_height = 400.0 var in_water : bool = false var swim_speed : float = 400.0 +var climb_speed : float = 5.0 # Control var controlling_machine = false #whether character is riding/controlling something var machine = null export var is_player = false #whether character is currently controlled by a player -var should_move = false var ladder_m = null #physics var player_state : PhysicsDirectBodyState var is_on_floor:bool -export(float) var acceleration = 80.0 -export(int) var walk_speed = 6 -export(float) var c_friction = 4.0 -export(float) var _airspeed_cap = 1.0 -export(float) var air_control = 1.0 +var acceleration = 80.0 +export(int) var walk_speed = 5 +var c_friction = 4.0 +var air_control = 0.3 # Called when the node enters the scene tree func _ready() -> void: @@ -174,10 +174,13 @@ func on_floor_test() -> bool: func _integrate_forces(state) -> void: player_state = state velocity = state.get_linear_velocity() - if should_move: - nav.set_velocity(velocity) - if nav.is_target_reached(): - should_move = false + for i in range(player_state.get_contact_count()): + var contact_angle_from_up : float = Vector3.UP.angle_to(player_state.get_contact_local_normal(i)) + if contact_angle_from_up > FLOOR_MAX_ANGLE and !is_on_floor: + friction = 0 + break + if i == player_state.get_contact_count() - 1: + friction = 1 # on input event func _input(event: InputEvent) -> void: @@ -191,16 +194,12 @@ func walk(_delta:float) -> void: direction = Vector3() var aim: Basis = head.get_global_transform().basis direction += -move_axis.x * aim.z + move_axis.y * aim.x - if !is_player and should_move: - direction = nav.get_next_location() - global_transform.origin - if nav.get_next_location().y - global_transform.origin.y > 0.05 and is_on_floor: - apply_central_impulse(Vector3.UP*jump_height) direction.y = 0 direction = direction.normalized() # Jump - if is_on_floor and is_player and jumping: - apply_central_impulse(Vector3.UP*jump_height) + if is_player and jumping and is_on_floor and can_jump: + jump() #max walk speed var _speed = walk_speed @@ -213,13 +212,20 @@ func walk(_delta:float) -> void: var projVel = Vector2(velocity.x-floorspeed.x,velocity.z-floorspeed.z).dot(Vector2(direction.x,direction.z)) if is_on_floor: + add_central_force(-mass*linear_velocity.normalized()*c_friction)#friction if _speed - _cspeed > 0: add_central_force (mass*Vector3(direction.x*_temp_accel, 0, direction.z*_temp_accel))#velocity.x += direction.x*_temp_accel else: add_central_force(mass*Vector3(direction.x*(_speed-projVel), 0, direction.z*(_speed-projVel))) - elif _airspeed_cap - projVel > 0: + elif 1.0 - projVel > 0: add_central_force (mass*Vector3(direction.x*_temp_accel, 0, direction.z*_temp_accel)) +func jump(): + can_jump = false + apply_central_impulse(Vector3.UP*jump_height) + yield(get_tree().create_timer(0.05),"timeout") + can_jump = true + func swim(_delta): #drag and buoyancy add_central_force(Vector3.UP*weight*1.0) @@ -255,7 +261,7 @@ func mount_ladder(target_ladder): #called each frame while climbing ladder func climb_ladder(delta): - var new_ladder_pos = ladder_m.global_transform.origin + ladder_m.global_transform.basis.y.normalized() * move_axis.x * delta + var new_ladder_pos = ladder_m.global_transform.origin + ladder_m.global_transform.basis.y.normalized() * move_axis.x * delta * climb_speed var prog = ladder_m.get_parent().get_climb_scalar(new_ladder_pos) if prog >= 0.0 and prog <= 1.0: ladder_m.global_transform.origin = new_ladder_pos |
