AI now looks at player. Walking towards player is still being implemented but WIP code is there

This commit is contained in:
Paul Black 2022-03-05 23:33:48 -07:00
parent bc16fd0513
commit 6d5e7d23da
4 changed files with 13 additions and 9 deletions

View file

@ -3,16 +3,17 @@ extends KinematicBody
var speed = 15
var gravity = 100
var velocity = Vector3.ZERO
onready var player = find_node("PlayerTank") ## FIXME
# onready var player = find_node("PlayerTank") ## FIXME
onready var player = get_node("/root/Spatial/PlayerTank/CollisionShape")
func _physics_process(delta):
var direction = Vector3.ZERO
velocity.x = direction.x * speed
velocity.z = direction.z * speed
velocity.y -= gravity * delta
velocity = move_and_slide(velocity, Vector3.UP)
if player != null: # Makes sure player is not an empty variable (not equal to nill/null)
look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0))
$CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0))
direction.z -= 1 ### CODE FROM HERE DOWN NEEDS IMPROVEMENT (AI walks up nonstop and not towards player...)
velocity.x = direction.x * speed
velocity.z = direction.z * speed
velocity.y -= gravity * delta
velocity = move_and_slide(velocity, Vector3.UP)

View file

@ -11,7 +11,6 @@ var velocity = Vector3.ZERO
func _physics_process(delta):
Global.player = self # Makes the player variable in the global script equal to self
var direction = Vector3.ZERO
### Input management
if Input.is_action_pressed("move_right"):
direction.x += 1

View file

@ -15,7 +15,7 @@ script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 20.7405, -7.28392e-07 )
[node name="CollisionShape" type="CollisionShape" parent="." groups=["tank"]]
[node name="CollisionShape" type="CollisionShape" parent="." groups=["player", "tank"]]
shape = SubResource( 1 )
[node name="Tank" type="MeshInstance" parent="CollisionShape" groups=["tank"]]

View file

@ -44,21 +44,25 @@ singletons=[ "res://addons/godot-git-plugin/git_api.gdnlib" ]
move_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
move_back={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
move_forward={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
shoot={