Made tank turn with direction of velocity. Improved how a tank is considered AI for bullet checks.

This commit is contained in:
Paul Black 2022-03-10 22:06:26 -07:00
parent 5437b1c6f3
commit 09c44ca20b
10 changed files with 43 additions and 20 deletions

View file

@ -0,0 +1,3 @@
source_md5="edf0227dc83d529ddc426be0b2307f74"
dest_md5="4317eabf26800407c4217e94f37f0a79"

View file

@ -0,0 +1,3 @@
source_md5="9eab99d70ccd721911d0e95cb3dc0e6a"
dest_md5="be88d9e1f653c3a962dd7f787001613f"

View file

@ -0,0 +1,3 @@
source_md5="d625b5879f6e5866b73469f9679decaf"
dest_md5="1951b3507ff7167a76d0d40074c3c646"

Binary file not shown.

View file

@ -16,6 +16,7 @@ var ablewalk = 0
var speed = 10
var gravity = 100
var velocity = Vector3.ZERO
var angular_acceleration = 7 # How fast the tank rotates
onready var player = get_node("/root/Spatial/PlayerTank")
func _ready():
@ -44,12 +45,13 @@ func _physics_process(delta):
if ablewalk == 0:
_walk_towards_player()
_look_at_player()
$CollisionShapeBottom.rotation.y = lerp_angle($CollisionShapeBottom.rotation.y, atan2(velocity.x, velocity.z), delta * angular_acceleration)
func _bullet_fire():
if player != null:
var b = Bullet.instance()
owner.add_child(b)
b.transform = $CollisionShape/Tank/BulletGenerator.global_transform
b.transform = $CollisionShapeTop/Top_tank/BulletGenerator.global_transform
b.velocity = -b.global_transform.basis.z * b.muzzle_velocity
bulletrandomnum.randomize()
@ -63,7 +65,7 @@ func _walk_towards_player():
func _look_at_player():
if player != null:
$CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) # Look at player
$CollisionShapeTop.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) # Look at player
func _walk_towards_player_timer():
walkingrandomnum.randomize()

View file

@ -1,24 +1,31 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://Objects/Ai-tank/tank00.gd" type="Script" id=1]
[ext_resource path="res://Objects/Player-tank/model/Bottom_tank.dae" type="PackedScene" id=2]
[ext_resource path="res://Objects/Player-tank/model/Top_tank.dae" type="PackedScene" id=3]
[sub_resource type="BoxShape" id=1]
extents = Vector3( 1.29, 1, 2.428 )
[sub_resource type="CubeMesh" id=2]
[sub_resource type="SpatialMaterial" id=3]
albedo_color = Color( 1, 0, 0, 1 )
[sub_resource type="BoxShape" id=2]
[node name="AITank00" type="KinematicBody"]
script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="." groups=["tank"]]
[node name="CollisionShapeBottom" type="CollisionShape" parent="." groups=["player", "tank"]]
shape = SubResource( 1 )
[node name="Tank" type="MeshInstance" parent="CollisionShape" groups=["tank"]]
mesh = SubResource( 2 )
skeleton = NodePath("../..")
material/0 = SubResource( 3 )
[node name="Bottom_tank" parent="CollisionShapeBottom" instance=ExtResource( 2 )]
transform = Transform( -1.44248e-06, 0, 33, 0, 33, 0, -33, 0, -1.44248e-06, 0, -1.90625, 0 )
[node name="BulletGenerator" type="Position3D" parent="CollisionShape/Tank"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0032903, 0.375606, -1.3599 )
[node name="CollisionShapeTop" type="CollisionShape" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.572825, 0 )
shape = SubResource( 2 )
[node name="Top_tank" parent="CollisionShapeTop" instance=ExtResource( 3 )]
transform = Transform( -1.44248e-06, 0, 33, 0, 33, 0, -33, 0, -1.44248e-06, 0, -2.74403, 0 )
[node name="BulletGenerator" type="Position3D" parent="CollisionShapeTop/Top_tank"]
transform = Transform( -1.32459e-09, 0, -0.030303, 0, 0.030303, 0, 0.030303, 0, -1.32459e-09, 0.124162, 0.0831104, -9.97006e-05 )
[node name="TankAI" type="Node" parent="."]

View file

@ -2,7 +2,7 @@ extends Area
signal exploded
export var muzzle_velocity = 25
export var muzzle_velocity = 25 # How fast the bullets are
export var g = Vector3.DOWN * 20
var velocity = Vector3.ZERO
@ -18,10 +18,12 @@ func _physics_process(delta):
print ("Found Player Tank")
self.queue_free()
body.queue_free()
if get_overlapping_bodies().has(get_parent().get_node("AITank00")):
print ("Found AI Tank")
self.queue_free()
body.queue_free()
else:
if get_overlapping_bodies():
if body.has_node("TankAI"):
print ("Found AI Tank")
self.queue_free()
body.queue_free()
func _on_Shell_body_entered(body):

View file

@ -6,6 +6,7 @@ var rayOrigin = Vector3()
var rayEnd = Vector3()
var speed = 15
var gravity = 100
var angular_acceleration = 7 # How fast the player rotates
var velocity = Vector3.ZERO
func _physics_process(delta):
@ -33,12 +34,14 @@ func _physics_process(delta):
var intersection = space_state.intersect_ray(rayOrigin, rayEnd) # Set variable for cursor position
if not intersection.empty(): # Make sure there is a intersection with raycast
var pos = intersection.position
$CollisionShapeTop.look_at(Vector3(pos.x, pos.y, pos.z), Vector3(0,1,0))
$CollisionShapeTop.look_at(Vector3(pos.x, $CollisionShapeTop.translation.y, pos.z), Vector3(0,1,0))
if direction != Vector3.ZERO:
direction = direction.normalized()
# $Pivot.look_at(translation + direction, Vector3.UP)
$CollisionShapeBottom.rotation.y = lerp_angle($CollisionShapeBottom.rotation.y, atan2(direction.x, direction.z), delta * angular_acceleration)
velocity.x = direction.x * speed
velocity.z = direction.z * speed
velocity.y -= gravity * delta