Godot Version
v4.2.1.stable.official [b09f793f5]
Question
I am trying to use the move_and_slide()
in my code. How can I implement this?
CONTEXT
I am making an RTS game and this is the start of my script that I am using to make it. This script is labeled “skrimish_master” and is being used to control what happens in-match as the player is playing. I can get my “Actors” (my RTS units) to look_at my right mouse click, context_GameObject.look_at(Vector3(results.position.x, 0, results.position.z))
, but I cant get the Actor to move towards the right click in a smooth manner. It only jumps to its location.
move_and_slide()
isn’t working to make the unit move to the right click location.
My unit is created using a class Actor and is stores in a Dictionary called teams_skrimish_actors_DICTIONARY[squad_#]
as Actor_#
so it then looks like teams_skrimish_actors_DICTIONARY[squad_#[Actor_#]]
I can get the
CODE / SCRIPT
extends Node
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@onready var formation_type
@onready var selected_actors: Array
@onready var mouse_pos
@onready var camera_ref:Camera3D = $"../Camera3D"
@onready var results
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#this is the currently selected Game Object that exists in the world
@onready var context_GameObject = null
@onready var context_GameObject_Group = null
@onready var building_rally_point:Vector3
@onready var delta_ref
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@onready var actor_GameObject = preload("res://Assets/GAME_READY/UNITS/HUMANS/unit_static_body_3d.tscn")
@onready var actors_container_ref = $Actors_container
@onready var squad_count = 0
@onready var teams_skrimish_actors_DICTIONARY = {
"solo_actors": {
},
"squad_1": {
}
}
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@onready var Action_Button_Menu_Ref: Dictionary = {
"Button_1": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_1_HBoxContainer/Action_Button_1,
"Button_2": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_1_HBoxContainer/Action_Button_2,
"Button_3": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_1_HBoxContainer/Action_Button_3,
"Button_4": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_1_HBoxContainer/Action_Button_4,
"Button_5": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_2_HBoxContainer/Action_Button_5,
"Button_6": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_2_HBoxContainer/Action_Button_6,
"Button_7": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_2_HBoxContainer/Action_Button_7,
"Button_8": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_2_HBoxContainer/Action_Button_8,
"Button_9": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_3_HBoxContainer/Action_Button_9,
"Button_10": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_3_HBoxContainer/Action_Button_10,
"Button_11": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_3_HBoxContainer/Action_Button_11,
"Button_12": $SKRIMISH_UI/Action_Menu_Margin/Action_Button_Menu/VBoxContainer/Action_Row_3_HBoxContainer/Action_Button_12,
}
@onready var GUI_icons: Dictionary = {
"back_icon": preload("res://UserInterface/back_Arrow.svg"),
"blank_icon": preload("res://UserInterface/BLANK_Icon.png"),
"build_icon": preload("res://UserInterface/build_icon.svg"),
"delete_icon": preload("res://UserInterface/Delete_Icon.png"),
"godot_icon": preload("res://UserInterface/icon.svg"),
"move_icon": preload("res://UserInterface/Move_Icon.png"),
"stop_icon": preload("res://UserInterface/Stop_Icon.png"),
"tile_icon": preload("res://UserInterface/tile.svg"),
}
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func _ready() -> void:
for button in Action_Button_Menu_Ref:
Action_Button_Menu_Ref[button].texture_normal = preload("res://UserInterface/BLANK_Icon.png")
func _process(delta: float) -> void:
delta_ref = delta
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@onready var square_formation_positions:Dictionary = {}
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Actor:
var name: String
var id: int
var group_id: int
var level: int = 1
var level_progress: int = 0
var position: Vector3
var health: int = 120
var moral: int = 100
var armour: int = 25
var speed: int = 15
var weapon: String
var faction: String
var party: String
var skin: Color
func _init(_name: String, _id: int, _group_id: int, _level: int, _level_progress: float, _position: Vector3, _health: int, _moral: int, _armour: int, _speed: int, _weapon: String, _faction: String, _party: String, _skin: Color):
name = _name
id = _id
group_id = _group_id
level = _level
level_progress = _level_progress
position = _position
health = _health
moral = _moral
armour = _armour
speed = _speed
weapon = _weapon
faction = _faction
party = _party
skin = _skin
self.material_overide = skin
func move(where: Vector3, to: Vector3):
# should be move_and_slide() but cant figure it out yet
pass
func attack(who: Actor):
# Should right click a location and if it is an enemy, attack, else repair, heal, move etc.
pass
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Squad:
var actor_name = "Actor"+str(ID_Count)
var ID_Count = 0
var group_ID = 0
var squad_position = null
var faction: String = "Faction_1"
var party: String = "Faction_1"
var formation: String = "Square"
var squad_dictionary: Dictionary = {}
func spawn_actor(actor_type:String):
var actor
match actor_type:
"Assault":
squad_dictionary[ID_Count] = Actor.new(actor_name, ID_Count, group_ID, 1, 0, squad_position, 120, 150, 50, 15, weapon("rifle"), faction, party, Color.YELLOW)
ID_Count += 1
"Engineer":
squad_dictionary[ID_Count] = Actor.new(actor_name, ID_Count, group_ID, 1, 0, squad_position, 90, 170, 40, 10, weapon("shotgun"), faction, party, Color.GREEN)
ID_Count += 1
_:
print("Invalid actor type")
return null
return actor
#func _physics_process(delta):
#Vector3(self.transform.basis.z).normalized() * delta
#move_and_slide()
#pass
func set_formation(type: String):
var squad_leader = squad_dictionary[0]
match type:
"square":
var actor_offset:int = 5
var layer_offset:int = 10
squad_dictionary["Position_1"] = squad_dictionary[0].position
squad_dictionary["Position_2"] = Vector3(squad_leader.position.x - actor_offset, 0, squad_leader.position.z) # L
squad_dictionary["Position_3"] = Vector3(squad_leader.position.x + actor_offset, 0, squad_leader.position.z) # R
squad_dictionary["Position_4"] = Vector3(squad_leader.position.x, 0, squad_leader.position.z + actor_offset) # U
squad_dictionary["Position_5"] = Vector3(squad_leader.position.x, 0, squad_leader.position.z - actor_offset) # D
squad_dictionary["Position_6"] = Vector3(squad_leader.position.x - actor_offset, 0, squad_leader.position.z + actor_offset) # LU
squad_dictionary["Position_7"] = Vector3(squad_leader.position.x + actor_offset, 0, squad_leader.position.z + actor_offset) # RU
squad_dictionary["Position_8"] = Vector3(squad_leader.position.x - actor_offset, 0, squad_leader.position.z - actor_offset) # LD
squad_dictionary["Position_9"] = Vector3(squad_leader.position.x + actor_offset, 0, squad_leader.position.z - actor_offset) # RD
squad_dictionary["Position_10"] = Vector3(squad_leader.position.x - layer_offset, 0, squad_leader.position.z) # LL
squad_dictionary["Position_11"] = Vector3(squad_leader.position.x + layer_offset, 0, squad_leader.position.z) # RR
squad_dictionary["Position_12"] = Vector3(squad_leader.position.x, 0, squad_leader.position.z + layer_offset) # UU
squad_dictionary["Position_13"] = Vector3(squad_leader.position.x, 0, squad_leader.position.z - layer_offset) # DD
squad_dictionary["Position_14"] = Vector3(squad_leader.position.x + actor_offset, 0, squad_leader.position.z - layer_offset) # UL
squad_dictionary["Position_15"] = Vector3(squad_leader.position.x - actor_offset, 0, squad_leader.position.z - layer_offset) # UR
squad_dictionary["Position_16"] = Vector3(squad_leader.position.x + actor_offset, 0, squad_leader.position.z + layer_offset) # DL
squad_dictionary["Position_17"] = Vector3(squad_leader.position.x - actor_offset, 0, squad_leader.position.z + layer_offset) # DR
squad_dictionary["Position_18"] = Vector3(squad_leader.position.x - layer_offset, 0, squad_leader.position.z + actor_offset) # LL U
squad_dictionary["Position_19"] = Vector3(squad_leader.position.x - layer_offset, 0, squad_leader.position.z - actor_offset) # LL D
squad_dictionary["Position_20"] = Vector3(squad_leader.position.x - layer_offset, 0, squad_leader.position.z + layer_offset) # LL UU
squad_dictionary["Position_21"] = Vector3(squad_leader.position.x - layer_offset, 0, squad_leader.position.z - layer_offset) # LL DD
squad_dictionary["Position_22"] = Vector3(squad_leader.position.x + layer_offset, 0, squad_leader.position.z + actor_offset) # RR U
squad_dictionary["Position_23"] = Vector3(squad_leader.position.x + layer_offset, 0, squad_leader.position.z - actor_offset) # RR D
squad_dictionary["Position_24"] = Vector3(squad_leader.position.x + layer_offset, 0, squad_leader.position.z - layer_offset) # RR UU
squad_dictionary["Position_25"] = Vector3(squad_leader.position.x + layer_offset, 0, squad_leader.position.z + layer_offset) # RR DD
_:
pass
func generate_actor(from):
#the is linked to a button and is used to spawn an actor from a building or squad
pass
func weapon(type):
match type:
"rifle":
pass
"shotgun":
pass
_:
pass
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func delete():
pass
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func _input(event):
if event is InputEventMouseButton:
mouse_pos = event.position
rayCast()
if event.is_action_released("Left_Click"):
print("Left mouse button pressed")
if results.size() > 0:
print(results)
for group_name in results.collider.get_groups():
match group_name:
"Selectable":
pass
"Unit":
context_GameObject = results.collider
context_GameObject_Group = group_name
Action_Button_Menu_Ref["Button_1"].texture_normal = GUI_icons["move_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["build_icon"]
Action_Button_Menu_Ref["Button_4"].texture_normal = GUI_icons["stop_icon"]
Action_Button_Menu_Ref["Button_9"].texture_normal = GUI_icons["back_icon"]
Action_Button_Menu_Ref["Button_12"].texture_normal = GUI_icons["delete_icon"]
print("WAS Unit")
"Building":
context_GameObject = results.collider
context_GameObject_Group = group_name
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["godot_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["build_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["godot_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["back_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["godot_icon"]
print("WAS Building")
"Map":
context_GameObject = null
context_GameObject_Group = null
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["back_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
print("WAS Map")
_:
context_GameObject = null
context_GameObject_Group = null
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["back_icon"]
Action_Button_Menu_Ref["Button_3"].texture_normal = GUI_icons["blank_icon"]
#do button code here
if event.is_action_released("Right_Click"):
print("Right mouse button pressed")
if context_GameObject_Group != null:
match context_GameObject_Group:
"Unit":
#context_GameObject.move_and_slide()
print(context_GameObject.position)
print(results.position, "\n")
var round_context = Vector3(round(context_GameObject.position.x), round(context_GameObject.position.y), round(context_GameObject.position.z))
var round_results = Vector3(round(results.position.x), round(results.position.y), round(results.position.z))
context_GameObject.velocity = Vector3(0, 0, -1) * 15
context_GameObject.look_at(Vector3(results.position.x, 0, results.position.z))
print(context_GameObject.transform.basis.z)
#context_GameObject.transform.origin -= Vector3(context_GameObject.transform.basis.z).normalized()
#while round(context_GameObject.position) != round_results:
#while range(round(context_GameObject.position.x), round_results.x-1, round_results.x+1):
#context_GameObject.move_and_slide()
#while context_GameObject.position.x < round_results.x-.2 || round(context_GameObject.position.x) > round_results.x+.2:
#print("round_context: ", round(context_GameObject.position))
#print("round_results: ", round_results, "\n")
#context_GameObject.transform.origin -= Vector3(context_GameObject.transform.basis.z).normalized() * delta_ref
"Building":
pass
_:
pass
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
func rayCast():
var worldspace = camera_ref.get_world_3d().direct_space_state
var from = camera_ref.project_ray_origin(mouse_pos)
var to = camera_ref.project_position(mouse_pos, 1000)
results = worldspace.intersect_ray(PhysicsRayQueryParameters3D.create(from, to))
CONCLUSION
Hopfully someone that is smarted than me can help me solve this. I genuinly am not sure how to complete get over this hurtle.
Regards,
Self.name