Advertisement
Guest User

Unity->GDscript projectile motion - attempt 1

a guest
Mar 23rd, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const GRAVITY : float = 9.81
  2. const SHAPE_RAYCAST_RADIUS := 0.3
  3. const LINE_POINT_COUNT : int = 50           # numPoints
  4. const TIME_BETWEEN_POINTS : float = .45     # timeBetweenPoints
  5. const PROJECTILE_POWER = 8
  6.  
  7. var query = PhysicsShapeQueryParameters3D.new()
  8.  
  9. func _ready() -> void:
  10.     update_visualiser_position.connect(get_parent()._on_projectile_visualiser_set_position)
  11.    
  12.     query.shape_rid = PhysicsServer3D.sphere_shape_create()
  13.     query.exclude = [self]
  14.     PhysicsServer3D.shape_set_data(query.shape_rid, SHAPE_RAYCAST_RADIUS)
  15.  
  16.  
  17. func calculate_projectile():
  18.     var points : Array[Vector3] = []
  19.    
  20.     var starting_pos : Vector3 = Vector3(0, 1, -1)
  21.     var starting_velocity : Vector3 = (basis.z * PROJECTILE_POWER)
  22.  
  23.     var time := 0.0
  24.     for n in LINE_POINT_COUNT:
  25.         # This is how the Unity tutorials calculated both the x and z - but it doesn't seem to work very well:
  26.         var new_point  : Vector3 = starting_pos + time * starting_velocity  
  27.  
  28.         new_point.y = starting_pos.y + starting_velocity.y * time + (GRAVITY / 2.0 + pow(time, 2))
  29.        
  30.         # As new_point is local to Player, we move the query's location by the players position + new_point
  31.         query.transform = global_transform.translated_local(new_point)
  32.        
  33.         var intersection := get_world_3d().direct_space_state.intersect_shape(query, 2)
  34.         if not intersection.is_empty():
  35.             # Get the point where the shape first intersected
  36.             var collision := get_world_3d().direct_space_state.get_rest_info(query)
  37.             if !collision.is_empty():
  38.                 # Location of point found, place an indicator on the point.
  39.                 pass
  40.             break
  41.  
  42.         points.append(new_point)
  43.         time += TIME_BETWEEN_POINTS
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement