extends RigidBody ###################-VARIABLES-#################### # Camera export(float) var mouse_sensitivity = 12.0 export(NodePath) var cam_path export(NodePath) var ai_follower = null export(float) var FOV = 90.0 var mouse_axis := Vector2() onready var cam: Camera = get_node(cam_path) # Move var velocity := Vector3() var direction := Vector3() var move_axis := Vector2() # Walk const FLOOR_MAX_ANGLE: float = deg2rad(46.0) export(float) var gravity = 17.35 export(float) var jump_height = 1.084 var jump_vel = 5.89#sqrt(2 * gravity * jump_height) var is_on_floor : bool = false onready var feet = get_node("Feet") var controlling_vehicle = false var vehicle = null export(float) var acceleration = 70 export(int) var walk_speed = 6 export(float) var _airspeed_cap = 1 export(float) var air_control = 1 # Fly export(int) var fly_speed = 50 export(int) var fly_accel = 10 var flying := false # Crouch var is_duck = 0 const duck_height = .5*.6*1.8 #one half the hitbox duck height var duck_speed = 2.3 var knockback_mult = 1 var duck_progress = 0 var world # Shoot var countdown = 0; export(float) var firing_cooldown = .8 export(Vector3) var snap static func compare_floats(a, b, epsilon = 0.00001): return abs(a - b) <= epsilon func walk(delta): knockback_mult = 1.35 if is_duck else 1 #check if is on floor if feet.is_colliding(): is_on_floor = true # Duck if not compare_floats(duck_progress, is_duck): var h = $CollisionBox.get_shape().get_extents().y var dir = -1 if is_duck else 1 var dh = dir*duck_speed*state.step var desh = duck_height if is_duck else 0.9 if abs(h+dh - desh) < .05: dh = desh-h $CollisionBox.get_shape().set_extents(Vector3(.325,h+dh,.325)) $CollisionBox.translate_object_local(Vector3(0,-dh/2,0)) duck_progress = 1-(h+dh-duck_height)/(0.9-duck_height) # Input direction = Vector3() var aim: Basis = get_global_transform().basis if move_axis.x >= 0.5: direction -= aim.z elif move_axis.x <= -0.5: direction += aim.z if move_axis.y <= -0.5: direction -= aim.x elif move_axis.y >= 0.5: direction += aim.x direction.y = 0 direction = direction.normalized() # Jump if is_on_floor: snap = Vector3.DOWN if Input.is_action_just_pressed("move_jump"): snap = Vector3.ZERO velocity.y += jump_vel velocity.y -= gravity * state.step if abs(velocity.y) > 1: snap = Vector3.ZERO #max walk speed var _speed = walk_speed if not is_duck else walk_speed*0.5 var _temp_accel: float = acceleration if not is_on_floor or Input.is_action_just_pressed("move_jump"): _temp_accel *= air_control else: #apply friction var _cspeed = sqrt(pow(velocity.x,2)+pow(velocity.z,2)) if _cspeed != 0: var walkdir = Vector2(velocity.x,velocity.z) var fvar = _cspeed*friction if direction.length() != 0 else _cspeed*2*friction #fvar *= 4 if is_duck else 1 velocity.x -= walkdir.normalized().x*fvar*state.step velocity.z -= walkdir.normalized().y*fvar*state.step var projVel = Vector2(velocity.x,velocity.z).dot(Vector2(direction.x,direction.z)) _temp_accel *= state.step if is_on_floor: if _speed - (projVel + _temp_accel) > 0: velocity.x += direction.x*_temp_accel velocity.z += direction.z*_temp_accel else: velocity.x += direction.x*(_speed-projVel) velocity.z += direction.z*(_speed-projVel) elif _airspeed_cap - projVel > 0: velocity.x += direction.x*_temp_accel velocity.z += direction.z*_temp_accel # clamping (to stop on slopes) if direction.dot(velocity) == 0: var _vel_clamp := 0.25 if abs(velocity.x) < _vel_clamp: velocity.x = 0 if abs(velocity.z) < _vel_clamp: velocity.z = 0 # Move print(velocity) set_linear_velocity(velocity) velocity = get_linear_velocity()