cubio.gd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. extends KinematicBody
  2. # member variables here, example:
  3. # var a=2
  4. # var b="textvar"
  5. var g = -9.8
  6. var vel = Vector3()
  7. const MAX_SPEED = 5
  8. const JUMP_SPEED = 7
  9. const ACCEL= 2
  10. const DEACCEL= 4
  11. const MAX_SLOPE_ANGLE = 30
  12. func _fixed_process(delta):
  13. var dir = Vector3() #where does the player intend to walk to
  14. var cam_xform = get_node("target/camera").get_global_transform()
  15. if (Input.is_action_pressed("move_forward")):
  16. dir+=-cam_xform.basis[2]
  17. if (Input.is_action_pressed("move_backwards")):
  18. dir+=cam_xform.basis[2]
  19. if (Input.is_action_pressed("move_left")):
  20. dir+=-cam_xform.basis[0]
  21. if (Input.is_action_pressed("move_right")):
  22. dir+=cam_xform.basis[0]
  23. dir.y=0
  24. dir=dir.normalized()
  25. vel.y+=delta*g
  26. var hvel = vel
  27. hvel.y=0
  28. var target = dir*MAX_SPEED
  29. var accel
  30. if (dir.dot(hvel) >0):
  31. accel=ACCEL
  32. else:
  33. accel=DEACCEL
  34. hvel = hvel.linear_interpolate(target,accel*delta)
  35. vel.x=hvel.x;
  36. vel.z=hvel.z
  37. var motion = move(vel*delta)
  38. var on_floor = false
  39. var original_vel = vel
  40. var floor_velocity=Vector3()
  41. var attempts=4
  42. while(is_colliding() and attempts):
  43. var n=get_collision_normal()
  44. if ( rad2deg(acos(n.dot( Vector3(0,1,0)))) < MAX_SLOPE_ANGLE ):
  45. #if angle to the "up" vectors is < angle tolerance
  46. #char is on floor
  47. floor_velocity=get_collider_velocity()
  48. on_floor=true
  49. motion = n.slide(motion)
  50. vel = n.slide(vel)
  51. if (original_vel.dot(vel) > 0):
  52. #do not allow to slide towads the opposite direction we were coming from
  53. motion=move(motion)
  54. if (motion.length()<0.001):
  55. break
  56. attempts-=1
  57. if (on_floor and floor_velocity!=Vector3()):
  58. move(floor_velocity*delta)
  59. if (on_floor and Input.is_action_pressed("jump")):
  60. vel.y=JUMP_SPEED
  61. var crid = get_node("../elevator1").get_rid()
  62. # print(crid," : ",PS.body_get_state(crid,PS.BODY_STATE_TRANSFORM))
  63. func _ready():
  64. # Initalization here
  65. set_fixed_process(true)
  66. pass
  67. func _on_tcube_body_enter( body ):
  68. get_node("../ty").show()
  69. pass # replace with function body