Guest User

Code

a guest
Jan 21st, 2020
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends ARVRController
  2.  
  3. onready var grab_area = $Area
  4. onready var grab_raycast = $GrabCast
  5. onready var grab_pos = $GrabPos
  6. onready var controller_mesh = $Controller
  7. onready var Tele_raycast : RayCast = $TeleCast
  8.  
  9. var controller_vel = Vector3(0,0,0)
  10. var prior_controller_pos = Vector3(0,0,0)
  11. var prior_controller_vel = []
  12.  
  13. var held_object = null
  14. var held_object_data = {"mode":RigidBody.MODE_RIGID, "layer":1, "mask":1}
  15.  
  16. var grab_mode = "AREA"
  17. var tele_pos
  18. var tele_mesh
  19. var tele_button_down
  20.  
  21. const CONT_DEADZONE = 0.5
  22. const MOVEMENT_SPEED = 2.5
  23. #var directional_move = false
  24.  
  25. func _ready():
  26. tele_mesh = get_tree().root.get_node("Game/Teleport_ShowPos")
  27. tele_button_down = false
  28.  
  29. grab_mode = "AREA"
  30. get_node("Sleep_Area").connect("body_entered",self,"sleep_area_entered")
  31. get_node("Sleep_Area").connect("body_exited",self,"sleep_area_exited")
  32.  
  33. connect("button_pressed",self,"button_pressed")
  34. connect("button_release",self,"button_released")
  35.  
  36. func _physics_process(delta):
  37. if tele_button_down:
  38. Tele_raycast.force_raycast_update() # Maybe Error here?
  39. if Tele_raycast.is_colliding(): # Error Here. Won't detect Collision.
  40. print("Tele Raycast hit!")
  41. if Tele_raycast.get_collider() is StaticBody:
  42. print("Tele Raycast hit StaticBody!")
  43. if Tele_raycast.get_collision_normal().y >= 0.85:
  44. tele_pos = Tele_raycast.get_collision_point()
  45. print("Hit Location: ",tele_pos)
  46. tele_mesh.global_transform.origin = tele_pos
  47.  
  48. if get_is_active():
  49. controller_vel = Vector3(0,0,0)
  50. if prior_controller_vel.size() > 0:
  51. for vel in prior_controller_vel:
  52. controller_vel += vel
  53. controller_vel = controller_vel / prior_controller_vel.size()
  54. prior_controller_vel.append((global_transform.origin - prior_controller_pos)/delta)
  55. controller_vel += (global_transform.origin - prior_controller_pos) / delta
  56. prior_controller_pos = global_transform.origin
  57. if prior_controller_vel.size() >= 30:
  58. prior_controller_vel.remove(0)
  59.  
  60. if held_object:
  61. var held_scale = held_object.scale
  62. held_object.global_transform = grab_pos.global_transform
  63. held_object.scale = held_scale
  64.  
  65. # Directional Movement Code Here
  66.  
  67. func button_pressed(button_index):
  68. if button_index == 15:
  69. if held_object:
  70. if held_object.has_method("interact"):
  71. held_object.interact()
  72. else:
  73. if not tele_mesh.visible and not held_object:
  74. tele_button_down = true
  75. tele_mesh.visible = true
  76. Tele_raycast.visible = true
  77. if button_index == 2:
  78. if tele_button_down:
  79. return
  80. if not held_object:
  81. var RB = null
  82. if grab_mode == "AREA":
  83. var bodies = grab_area.get_overlapping_bodies()
  84. if len(bodies) > 0:
  85. for body in bodies:
  86. if body is RigidBody:
  87. if not "NO_PICKUP" in body:
  88. RB = body
  89. break
  90. elif grab_mode == "RAYCAST":
  91. grab_raycast.force_raycast_update()
  92. if grab_raycast.is_colliding():
  93. if grab_raycast.get_collider() is RigidBody and not "NO_PICKUP" in grab_raycast.gey_collider():
  94. RB = grab_raycast.get_collider()
  95. if RB:
  96. held_object = RB
  97. held_object_data["mode"] = held_object.mode
  98. held_object_data["layer"] = held_object.collision_layer
  99. held_object_data["mask"] = held_object.collision_mask
  100. held_object.mode = RigidBody.MODE_STATIC
  101. held_object.collision_layer = 0
  102. held_object.collision_mask = 0
  103. controller_mesh.visible = false
  104. grab_raycast.visible = false
  105. if held_object.has_method("picked_up"):
  106. held_object.picked_up()
  107. if "controller" in held_object:
  108. held_object.controller = self
  109. else:
  110. held_object.mode = held_object_data["mode"]
  111. held_object.collision_layer = held_object_data["layer"]
  112. held_object.collision_mask = held_object_data["mask"]
  113. held_object.apply_impulse(Vector3(0,0,0), controller_vel)
  114. if held_object.has_method("dropped"):
  115. held_object.dropped()
  116. if "controller" in held_object:
  117. held_object.controller = null
  118. held_object = null
  119. controller_mesh.visible = true
  120. if grab_mode == "RAYCAST":
  121. grab_raycast.visible = true;
  122. if button_index == 1:
  123. if grab_mode == "AREA":
  124. grab_mode = "RAYCAST"
  125. if not held_object:
  126. grab_raycast.visible = true
  127. else:
  128. grab_mode = "AREA"
  129. grab_raycast.visible = false
  130. print("Grab Mode changed to ",grab_mode," on ",get_name())
  131.  
  132. func button_released(button_index):
  133. if button_index == 15:
  134. if tele_button_down:
  135. if tele_pos and tele_mesh.visible:
  136. var camera_offset = get_parent().get_node("Player_Camera").global_transform.origin - get_parent().global_transform.origin
  137. camera_offset.y = 0
  138. get_parent().global_transform.origin = tele_pos - camera_offset
  139. tele_button_down = false
  140. tele_mesh.visible = false
  141. Tele_raycast.visible = false
  142. tele_pos = null
  143.  
  144. func sleep_area_entered(body):
  145. if "can_sleep" in body:
  146. body.can_sleep = false
  147. body.sleeping = false
  148. func sleep_area_exited(body):
  149. if "can_sleep" in body:
  150. body.can_sleep = false
RAW Paste Data