extends CharacterBody3D @onready var curr_state = "none" @onready var next_state = "idle" @onready var prev_state = "" @onready var nav = $NavigationAgent3D const SPEED = 5 var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") #we just add ourselves to the group player func _ready(): add_to_group("player") #this displays what state we are in in the label 3d func update_state_shower(): $StateShower.text = curr_state #called when we should move to a postion from cam script. func move_to(pos): nav.set_target_position(pos) next_state="moving" func move_continuously(delta): var targetPos = nav.get_next_path_position()
var direction = global_position.direction_to(targetPos)
if prev_state != curr_state: update_state_shower() match curr_state: "idle": if prev_state!=curr_state: var cam=GameManager.get_cam() cam.next_state="normal" $AnimationPlayer.play("Idle") "moving": if prev_state!=curr_state: $AnimationPlayer.play("Walk") move_continuously(delta) "throwing": if prev_state!=curr_state: var cam = GameManager.get_cam() cam.next_state="throwing" $AnimationPlayer.play("Throw") player_revolve_with_mouse()
"launch_projectile": if prev_state!=curr_state: $AnimationPlayer.play(("Luanch_Projectile")) func start_throw(): next_state="throwing" func player_revolve_with_mouse(): var cam = GameManager.get_cam() var mousePos = get_viewport().get_mouse_position() var raylength = 1000 var from = cam.project_ray_origin(mousePos) var to = from + cam.project_ray_normal(mousePos) * raylength var space = get_world_3d().direct_space_state var rayQuery = PhysicsRayQueryParameters3D.new() rayQuery.from = from rayQuery.to = to rayQuery.collide_with_areas = true var result = space.intersect_ray(rayQuery)
#print(result) if "position" in result: look_at(result.position) func _on_animation_player_animation_finished(anim_name): if anim_name=="Luanch_Projectile": next_state="idle" var cam = GameManager.get_cam() cam.next_state="normal"
extends Camera3D @export var speed = 10 func _ready(): GameManager.set_cam(self) func allow_camera_movement(delta): if Input.is_action_pressed("up"): position.y += speed * delta if Input.is_action_pressed("down"): position.y -= speed * delta if Input.is_action_pressed("left"): position.x -= speed * delta if Input.is_action_pressed("right"): position.x += speed * delta if Input.is_action_pressed("forward"): position.z -= speed * delta if Input.is_action_pressed("backward"): position.z += speed * delta if Input.is_action_pressed("rotate_down"): rotation.x += 0.25 * delta if Input.is_action_pressed("rotate_up"): rotation.x -= 0.25 * delta func allow_clicking(): if Input.is_action_just_pressed("click"): var rayhit = raycast_from_mouse() if !rayhit.is_empty(): print("We clicked on: ", rayhit.collider.name) if rayhit.collider.is_in_group("player"): GameManager.active_character_instance = rayhit.collider $CameraUI.update_aci_label() #active_character_instance.move_to(rayhit["position"]) if rayhit.collider.is_in_group("ground") and GameManager.active_character_instance: GameManager.active_character_instance.move_to(rayhit["position"])
func allow_throw_clicking(): if Input.is_action_just_pressed("click"): var rayhit = raycast_from_mouse() if !rayhit.is_empty(): print("We clicked on: ", rayhit.collider.name) if rayhit.collider.is_in_group("player"): GameManager.active_character_instance.next_state="launch_projectile" if rayhit.collider.is_in_group("ground") and GameManager.active_character_instance: GameManager.active_character_instance.next_state="launch_projectile" func raycast_from_mouse(): var m_pos = get_viewport().get_mouse_position() var ray_start = self.project_ray_origin(m_pos) var ray_end = ray_start + self.project_ray_normal(m_pos) * 1000 var world3d : World3D = get_world_3d() var space_state = world3d.direct_space_state if space_state == null: return var query = PhysicsRayQueryParameters3D.create(ray_start, ray_end) query.collide_with_areas = true return space_state.intersect_ray(query) @onready var current_state="??" @onready var next_state="normal" @onready var prev_state func _process(delta): allow_camera_movement(delta)
prev_state=current_state current_state=next_state if prev_state!=current_state: print("Simple Cam changing states to:", current_state) match current_state: "throwing": allow_throw_clicking() "normal": allow_clicking()
CAMERA UI SCRIPT extends Control @onready var aci_label=$ACI_Label#Active character instance @onready var cam_state=$CameraState func update_aci_label(): aci_label.text = str(GameManager.active_character_instance) func _on_throw_button_pressed(): GameManager.active_character_instance.start_throw()
I don't use godot, don't plan on using it, but I had to give a like for that thumbnail alone.
Wow thats clever and simple. Thank you
Glad it was helpful!
GAME MANAGER SCRIPT
extends Node
@onready var active_character_instance = null
@onready var cam:Camera3D
func set_cam(use_cam):
cam = use_cam
func get_cam():
return cam
extends CharacterBody3D
@onready var curr_state = "none"
@onready var next_state = "idle"
@onready var prev_state = ""
@onready var nav = $NavigationAgent3D
const SPEED = 5
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
#we just add ourselves to the group player
func _ready():
add_to_group("player")
#this displays what state we are in in the label 3d
func update_state_shower():
$StateShower.text = curr_state
#called when we should move to a postion from cam script.
func move_to(pos):
nav.set_target_position(pos)
next_state="moving"
func move_continuously(delta):
var targetPos = nav.get_next_path_position()
var direction = global_position.direction_to(targetPos)
look_at(targetPos, Vector3.UP)
velocity = direction * SPEED * delta
move_and_collide(velocity)
if nav.distance_to_target() < 1:
next_state = "idle"
func _physics_process(delta):
prev_state = curr_state
curr_state = next_state
if prev_state != curr_state:
update_state_shower()
match curr_state:
"idle":
if prev_state!=curr_state:
var cam=GameManager.get_cam()
cam.next_state="normal"
$AnimationPlayer.play("Idle")
"moving":
if prev_state!=curr_state:
$AnimationPlayer.play("Walk")
move_continuously(delta)
"throwing":
if prev_state!=curr_state:
var cam = GameManager.get_cam()
cam.next_state="throwing"
$AnimationPlayer.play("Throw")
player_revolve_with_mouse()
"launch_projectile":
if prev_state!=curr_state:
$AnimationPlayer.play(("Luanch_Projectile"))
func start_throw():
next_state="throwing"
func player_revolve_with_mouse():
var cam = GameManager.get_cam()
var mousePos = get_viewport().get_mouse_position()
var raylength = 1000
var from = cam.project_ray_origin(mousePos)
var to = from + cam.project_ray_normal(mousePos) * raylength
var space = get_world_3d().direct_space_state
var rayQuery = PhysicsRayQueryParameters3D.new()
rayQuery.from = from
rayQuery.to = to
rayQuery.collide_with_areas = true
var result = space.intersect_ray(rayQuery)
#print(result)
if "position" in result:
look_at(result.position)
func _on_animation_player_animation_finished(anim_name):
if anim_name=="Luanch_Projectile":
next_state="idle"
var cam = GameManager.get_cam()
cam.next_state="normal"
baldo's gate
extends Camera3D
@export var speed = 10
func _ready():
GameManager.set_cam(self)
func allow_camera_movement(delta):
if Input.is_action_pressed("up"):
position.y += speed * delta
if Input.is_action_pressed("down"):
position.y -= speed * delta
if Input.is_action_pressed("left"):
position.x -= speed * delta
if Input.is_action_pressed("right"):
position.x += speed * delta
if Input.is_action_pressed("forward"):
position.z -= speed * delta
if Input.is_action_pressed("backward"):
position.z += speed * delta
if Input.is_action_pressed("rotate_down"):
rotation.x += 0.25 * delta
if Input.is_action_pressed("rotate_up"):
rotation.x -= 0.25 * delta
func allow_clicking():
if Input.is_action_just_pressed("click"):
var rayhit = raycast_from_mouse()
if !rayhit.is_empty():
print("We clicked on: ", rayhit.collider.name)
if rayhit.collider.is_in_group("player"):
GameManager.active_character_instance = rayhit.collider
$CameraUI.update_aci_label()
#active_character_instance.move_to(rayhit["position"])
if rayhit.collider.is_in_group("ground") and GameManager.active_character_instance:
GameManager.active_character_instance.move_to(rayhit["position"])
func allow_throw_clicking():
if Input.is_action_just_pressed("click"):
var rayhit = raycast_from_mouse()
if !rayhit.is_empty():
print("We clicked on: ", rayhit.collider.name)
if rayhit.collider.is_in_group("player"):
GameManager.active_character_instance.next_state="launch_projectile"
if rayhit.collider.is_in_group("ground") and GameManager.active_character_instance:
GameManager.active_character_instance.next_state="launch_projectile"
func raycast_from_mouse():
var m_pos = get_viewport().get_mouse_position()
var ray_start = self.project_ray_origin(m_pos)
var ray_end = ray_start + self.project_ray_normal(m_pos) * 1000
var world3d : World3D = get_world_3d()
var space_state = world3d.direct_space_state
if space_state == null:
return
var query = PhysicsRayQueryParameters3D.create(ray_start, ray_end)
query.collide_with_areas = true
return space_state.intersect_ray(query)
@onready var current_state="??"
@onready var next_state="normal"
@onready var prev_state
func _process(delta):
allow_camera_movement(delta)
prev_state=current_state
current_state=next_state
if prev_state!=current_state:
print("Simple Cam changing states to:", current_state)
match current_state:
"throwing":
allow_throw_clicking()
"normal":
allow_clicking()
👍
CAMERA UI SCRIPT
extends Control
@onready var aci_label=$ACI_Label#Active character instance
@onready var cam_state=$CameraState
func update_aci_label():
aci_label.text = str(GameManager.active_character_instance)
func _on_throw_button_pressed():
GameManager.active_character_instance.start_throw()