|
@@ -1,101 +1,135 @@
|
|
|
-extends KinematicBody
|
|
|
-
|
|
|
-
|
|
|
-# physics
|
|
|
-var moveSpeed : float = 5.0
|
|
|
+#Johann Massyn, 23/06/2019
|
|
|
+#Godot (3.1) 3D Player movement script
|
|
|
|
|
|
-# cam look
|
|
|
-var minLookAngle : float = -90.0
|
|
|
-var maxLookAngle : float = 90.0
|
|
|
-var lookSensitivity : float = 10.0
|
|
|
+#Atatch script to 3d node with following structure
|
|
|
+#Kinematic body: Player
|
|
|
+# - CollisionShape: CollisionShape
|
|
|
+# - Camera: Camera
|
|
|
|
|
|
-var ray: RayCast
|
|
|
+extends KinematicBody
|
|
|
|
|
|
-# vectors
|
|
|
-var vel : Vector3 = Vector3()
|
|
|
-var mouseDelta : Vector2 = Vector2()
|
|
|
+export (bool) var can_move = true #Alow player to input movment.
|
|
|
+export (bool) var can_sprint = true #Alow player to toggle sprint movment.
|
|
|
+export (float) var move_speed = 8 #Players movement speed
|
|
|
+export (float) var move_speed_sprint = 16 #Players sprint movement speed
|
|
|
+export (bool) var move_sprint = false #Player sprinting toggle
|
|
|
+export (float) var move_acceleration = 7 #Players acceleration to movment speed
|
|
|
+export (float) var move_deacceleration = 10 #Players deacceleration from movment speed
|
|
|
+export (bool) var mouse_captured = true #Toggles mouse captured mode
|
|
|
+export (float) var mouse_sensitivity_x = 0.3 #Mouse sensitivity X axis
|
|
|
+export (float) var mouse_sensitivity_y = 0.3 #Mouse sensitivity Y axis
|
|
|
+export (float) var mouse_max_up = 90 #Mouse max look angle up
|
|
|
+export (float) var mouse_max_down = -80 #Mouse max look angle down
|
|
|
+export (float) var Jump_speed = 6 #Players jumps speed
|
|
|
+export (bool) var allow_fall_input = true #Alow player to input movment when falling
|
|
|
+export (bool) var stop_on_slope = false #Toggle sliding on slopes
|
|
|
+export (float) var max_slides = 4 #Maximum of slides
|
|
|
+export (float) var floor_max_angle = 60 #Maximum slop angle player can traverse
|
|
|
+export (bool) var infinite_inertia = false #Toggle infinite inertia
|
|
|
+export (float) var gravaty = 1.625 #9.81 #Gravaty acceleration
|
|
|
+export (Vector3) var gravaty_vector = Vector3(0, -1, 0) #Gravaty normal direction vector
|
|
|
+export (Vector3) var floor_normal = Vector3(0, 1, 0) #Floor normal direction vector
|
|
|
+export (Vector3) var jump_vector = Vector3(0, 1, 0) #Jump normal direction vector
|
|
|
+export (Vector3) var velocity = Vector3(0, 0, 0) #Initial velocity
|
|
|
|
|
|
-onready var camera : Camera = get_node("Camera")
|
|
|
+onready var up = global_transform.origin.normalized()
|
|
|
+onready var forward = global_transform.basis.y
|
|
|
+onready var right = forward.cross(up).normalized()
|
|
|
|
|
|
func _ready():
|
|
|
- move_and_slide(Common.latLonToGlobal(0,0,1750), Vector3.UP)
|
|
|
- ray = RayCast.new()
|
|
|
- .get_parent().add_child(ray)
|
|
|
+ move_and_slide(Common.latLonToGlobal(Vector3(0,0,1750)), Vector3.UP)
|
|
|
+ if mouse_captured:
|
|
|
+ Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
+ pass
|
|
|
|
|
|
func _process(delta):
|
|
|
- # rotate the camera along the x axis
|
|
|
- camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta
|
|
|
-
|
|
|
- # clamp camera x rotation axis
|
|
|
- camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)
|
|
|
-
|
|
|
- # rotate the player along their y-axis
|
|
|
- rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta
|
|
|
-
|
|
|
- # reset the mouseDelta vector
|
|
|
- mouseDelta = Vector2()
|
|
|
- var origin = get_global_transform().origin
|
|
|
- .get_parent().get_node("UI/2").text="latlonhight: "+str(Common.globalToLatLon(origin.x,origin.y,origin.z, origin.length()))+","+str(origin.length())
|
|
|
-
|
|
|
-func _input(event):
|
|
|
- if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
|
- mouseDelta = event.relative
|
|
|
+ var pos = self.global_transform.origin
|
|
|
+ .get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
|
|
|
+ .get_parent().get_node("UI/2").text="latlonhight: "+str(Common.globalToLatLon(pos))+","+str(pos.length())
|
|
|
|
|
|
+ floor_normal = pos.normalized()
|
|
|
+ gravaty_vector = floor_normal * -1
|
|
|
+ #self.transform.basis.y = floor_normal
|
|
|
+ #rotation_degrees.x = 90
|
|
|
+
|
|
|
func _physics_process(delta):
|
|
|
-
|
|
|
- # reset the x and z velocity
|
|
|
- vel.x = 0
|
|
|
- vel.y = 0
|
|
|
- vel.z = 0
|
|
|
|
|
|
- var input = Vector3()
|
|
|
+ #player movement XY
|
|
|
+ var dir = Vector3()
|
|
|
+ up = global_transform.origin.normalized()
|
|
|
+ right = forward.cross(up).normalized()
|
|
|
+ forward = up.cross(right).normalized()
|
|
|
|
|
|
- # movement inputs
|
|
|
- if Input.is_action_pressed("move_forward"):
|
|
|
- input.y -= 1
|
|
|
- if Input.is_action_pressed("move_backward"):
|
|
|
- input.y += 1
|
|
|
- if Input.is_action_pressed("move_left"):
|
|
|
- input.x -= 1
|
|
|
- if Input.is_action_pressed("move_right"):
|
|
|
- input.x += 1
|
|
|
- if Input.is_action_pressed("move_up"):
|
|
|
- input.z += 1
|
|
|
- if Input.is_action_pressed("move_down"):
|
|
|
- input.z -= 1
|
|
|
+ if can_move and (is_on_floor() or allow_fall_input):
|
|
|
+
|
|
|
+ #Left
|
|
|
+ if Input.is_action_pressed("move_left"):
|
|
|
+ dir -= right
|
|
|
+
|
|
|
+ #Right
|
|
|
+ if Input.is_action_pressed("move_right"):
|
|
|
+ dir += right
|
|
|
+
|
|
|
+ #Forward
|
|
|
+ if Input.is_action_pressed("move_forward"):
|
|
|
+ dir += forward
|
|
|
|
|
|
- input = input.normalized()
|
|
|
+ #Backwards
|
|
|
+ if Input.is_action_pressed("move_backward"):
|
|
|
+ dir -= forward
|
|
|
+
|
|
|
+ # Jump
|
|
|
+ if not is_on_floor():
|
|
|
+ if Input.is_action_pressed("move_up"):
|
|
|
+ dir += up
|
|
|
+ if Input.is_action_pressed("move_down"):
|
|
|
+ dir -= up
|
|
|
+
|
|
|
+ #Jump
|
|
|
+ #if Input.is_action_pressed("move_jump") and is_on_floor():
|
|
|
+ # velocity += jump_vector * Jump_speed - (jump_vector * -1).normalized() * velocity.dot(jump_vector * -1)
|
|
|
|
|
|
- # get the forward and right directions
|
|
|
- var forward = global_transform.basis.z
|
|
|
- var right = global_transform.basis.x
|
|
|
- var up = global_transform.basis.y
|
|
|
+ #Sprint toggle
|
|
|
+ if can_sprint and Input.is_action_just_pressed("move_sprint") and is_on_floor():
|
|
|
+ move_sprint = true
|
|
|
+
|
|
|
+ if can_sprint and not Input.is_action_pressed("move_sprint") and is_on_floor():
|
|
|
+ move_sprint = false
|
|
|
+
|
|
|
+ velocity = velocity.linear_interpolate(dir * move_speed, move_acceleration * delta)
|
|
|
+ if !is_on_floor():
|
|
|
+ velocity += gravaty_vector * gravaty * delta
|
|
|
+ velocity = move_and_slide(velocity, up)
|
|
|
|
|
|
- var relativeDir = (forward * input.y + right * input.x + up * input.z)
|
|
|
+ transform = transform.looking_at(global_transform.origin + forward, up)
|
|
|
|
|
|
- var moveSpeed2 = moveSpeed
|
|
|
- if Input.is_action_pressed("sprint"):
|
|
|
- moveSpeed2 = moveSpeed * 3
|
|
|
- # set the velocity
|
|
|
- vel.x = relativeDir.x * moveSpeed2
|
|
|
- vel.z = relativeDir.z * moveSpeed2
|
|
|
- vel.y = relativeDir.y * moveSpeed2
|
|
|
|
|
|
- var gravity = self.global_transform.origin.normalized()
|
|
|
- # move the player
|
|
|
- vel = move_and_slide(vel, Vector3.UP)
|
|
|
- #ray = ray.look_at(self.global_transform.origin, Vector3.UP)
|
|
|
+ #Smooth movement
|
|
|
+ #dir = transform.basis.xform(dir.normalized()) * (move_speed_sprint if move_sprint else move_speed)
|
|
|
+ #if is_on_floor() or dir != Vector3(0, 0, 0):
|
|
|
+ # var acceleration = move_acceleration if dir.dot(velocity) else move_deacceleration
|
|
|
+ # var vp = gravaty_vector.normalized() * velocity.dot(gravaty_vector)
|
|
|
+ # velocity = (velocity - vp).linear_interpolate(dir, acceleration * delta) + vp
|
|
|
|
|
|
- var pos = self.global_transform.origin
|
|
|
- var polPos = Common.globalToLatLon(pos.x,pos.y,pos.z)
|
|
|
- var rotationDeg=get_rotation_degrees().x
|
|
|
- var posNorm=abs(pos.normalized().y)
|
|
|
- var targetRot = posNorm * 90
|
|
|
- #if rotationDeg < targetRot:
|
|
|
- #rotate_x(1)
|
|
|
- #elif rotationDeg > targetRot:
|
|
|
- #rotate_x(-1)
|
|
|
+ #Gravaty
|
|
|
+ #if !is_on_floor():
|
|
|
+ # velocity += gravaty_vector * gravaty * delta
|
|
|
|
|
|
- .get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
|
|
|
- #.get_node("UI/3").text="LatLonPos=="+str(latLonToGlobal( self.global_transform.origin.x,self.global_transform.origin.y))
|
|
|
+ #Player move
|
|
|
+ #move_and_slide(velocity, floor_normal, stop_on_slope, max_slides, deg2rad(floor_max_angle), infinite_inertia)
|
|
|
|
|
|
+ #Player orientation
|
|
|
+ #var spos = Common.globalToLatLon(self.global_transform.origin)
|
|
|
+ #global_rotate(Vector3(1,0,0), deg2rad(spos.x))
|
|
|
+ #global_rotate(Vector3(0,1,0), deg2rad(spos.y))
|
|
|
+
|
|
|
+
|
|
|
+func _input(event):
|
|
|
+ #Mouse movement
|
|
|
+ if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
|
+ rotate_y(deg2rad(-event.relative.x * 0.3))
|
|
|
+ forward = forward.rotated(up, deg2rad(-event.relative.x * mouse_sensitivity_x))
|
|
|
+ #rotate_y(deg2rad(-event.relative.x * mouse_sensitivity_x))
|
|
|
+ #self.rotation_degrees.y += -event.relative.x * mouse_sensitivity_x
|
|
|
+ $Camera.rotation_degrees.x = clamp($Camera.rotation_degrees.x + -event.relative.y * mouse_sensitivity_y, mouse_max_down, mouse_max_up)
|
|
|
+ pass
|