diff options
-rw-r--r-- | Player.gd | 48 |
1 files changed, 17 insertions, 31 deletions
@@ -2,44 +2,30 @@ extends KinematicBody export var speed = 20 export var ang_speed = PI -export var mouse_dpi = 0.2 +export var mouse_sens = 0.2 export var pitch_min = -0.8 export var pitch_max = 0.8 -func joystick(neg_x: String,pos_x: String,neg_y: String,pos_y: String): - var joystick = Input.get_vector(neg_x,pos_x,neg_y,pos_y) - var joystrength = abs(joystick.length()) - if joystick != Vector2.ZERO: - joystick = joystick.normalized() - return Vector3(joystick.x,joystick.y,joystrength) - func _physics_process(delta): - var d_ang_speed = ang_speed * delta + var local_up = transform.basis.y.normalized() + var local_right = transform.basis.x.normalized() + var local_forward = -transform.basis.z.normalized() - var rotation = Transform(self.rotation, Vector3.ZERO) - var up = rotation * Vector3.UP - var forward = rotation * Vector3.FORWARD - var right = rotation * Vector3.RIGHT + var stick = Input.get_vector("move_left", "move_right", "move_back", "move_forward") + var vert = Input.get_axis("move_down", "move_up") - var stick = joystick("move_left", "move_right", "move_back", "move_forward") var movement = Vector3.ZERO - movement += right * stick.x * stick.z * speed - movement += forward * stick.y * stick.z * speed - if Input.is_action_pressed("move_up"): - movement += up * speed - if Input.is_action_pressed("move_down"): - movement -= up * speed + movement += local_right * stick.x * speed + movement += local_forward * stick.y * speed + movement += local_up * vert * speed movement = move_and_slide(movement, Vector3.UP) - stick = joystick("look_right", "look_left", "look_down", "look_up") + stick = Input.get_vector("look_right", "look_left", "look_down", "look_up") var mouse_movement = MouseInput.get_mouse_movement() - if stick.z == 0.0: - stick.z = mouse_movement.length() * mouse_dpi - mouse_movement = -mouse_movement.normalized() - stick.x = mouse_movement.x - stick.y = mouse_movement.y - var pitch = forward.dot(Vector3.UP) - if (pitch >= pitch_min and stick.y < 0) or (pitch <= pitch_max and stick.y > 0): - rotate(right, stick.y * stick.z * d_ang_speed) - rotate(Vector3.UP, stick.x * stick.z * d_ang_speed) - + if stick.length() == 0.0: + stick.x = -mouse_movement.x * mouse_sens + stick.y = -mouse_movement.y * mouse_sens + rotation.x += stick.y * ang_speed * delta + rotation.x = clamp(rotation.x, pitch_min, pitch_max) + rotation.y += stick.x * ang_speed * delta + rotation.y = wrapf(rotation.y, 0, TAU) |