jueves, 16 de mayo de 2024

Esc para salir del juego con godot 4.2 script de GDScript;

 extends WorldEnvironment



# Called when the node enters the scene tree for the first time.

func _ready():

pass # Replace with function body.




func _process(delta):

# Verifica si se presiona la tecla Escape

if Input.is_action_pressed("ui_cancel"):

# Salir del juego

get_tree().quit()

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
EXPLICACION: Cambie la configuracion de pantalla, para que el juego salga en 1920 por 1080, y a la vez ocupa toda la pantalla , sin este script no puedes salir del juego sin presionar Ctrl mas alt mas Supr 
que es lo que se hace para cerrar un programa en concreto que no sabes por donde pararlo.....

martes, 14 de mayo de 2024

GDScript para Godot 4.2 repaso breve de animacion repetitiva de personaje;

 extends Node3D


var loop_enabled = true  # Flag to control loop execution


# Called when the node enters the scene tree for the first time.

func _ready():

# Optionally, initialize things here that only need to happen once

pass


# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta):

if loop_enabled:

# Play the animation

$AnimationPlayer.play("LASBRAZEAS")


# Function to start the continuous loop

func start_loop():

loop_enabled = true


# Function to stop the continuous loop

func stop_loop():

loop_enabled = false

--------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
Explicacion : algunas veces la animacion de un personaje quiero que sea repetitiva y no pare esta es la mejor forma de conseguirlo y funciona de maravilla....aunque claro esta cada caso tiene su problema y su forma de resolverse las IA  son una ayuda fantastica, no te lo resuelve a la primera pero te facilita soluciones.......

sábado, 11 de mayo de 2024

Un metodo de hacer un cañon y moverlo con Godot 4.2 en 3d.;

 



En el video podeis ver un cañon que muevo por el escenario para godot 4.2 en 3d.

Se trata de moverlo con las teclas flechas , de girarlo con -A- y con -D- que salte al presionar espacio. Y que apunte girando en eje X presionando teclas -z- y tecla -x-.


Tenia un fallo que e conseguido corregir y era que al girar el cañon a izquierda o a derecha y presionar a la vez teclas -z- o -x- el cañon se torcia de una forma muy extraña y terminaba arrastrandose por el suelo o girando al reves.



Entoces y tras muchas pruebas y consultas tambien a una IA, e dividido el cañon en dos partes, una base que es la que lo mueve hacia adelante hacia atrás hacia izquierda y hacia derecha, que contiene un script propio que solo hace eso,

Y sobre la base esta el cañon que tiene un script para el solo que no interviene en la base principal que sirve solo para girar de izquierda a derecha y para inclinar y apuntar arriba y abajo.




En el padre (la base) que es un Characterbody3d esta este script:


extends CharacterBody3D


var rotate_speed = 10.1 # Rotation speed


const SPEED = 5.0 # Movement speed

const JUMP_VELOCITY = 14.5 # Jump velocity


var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")


func _ready():

# Replace with function body

pass


func _physics_process(delta):

if not is_on_floor():

velocity.y -= gravity * delta # Apply gravity


if Input.is_action_just_pressed("ui_accept") and is_on_floor():

velocity.y = JUMP_VELOCITY # Jump


var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") # Get input direction


var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()


if direction:

velocity.x = direction.x * SPEED # Move in X direction

velocity.z = direction.z * SPEED # Move in Z direction

else:

velocity.x = move_toward(velocity.x, 0, SPEED) # Stop horizontal movement

velocity.z = move_toward(velocity.z, 0, SPEED) # Stop vertical movement


move_and_slide() # Apply movement and collisions


var movimiento_vector = Vector3.ZERO # Initialize movement vector


if Input.is_action_pressed("A"): # Move forward

rotate_x(0.01) # Rotate on X axis (negative for down)

elif Input.is_action_pressed("B"): # Move backward

rotate_x(-0.01) # Rotate on X axis (positive for up)

elif Input.is_action_pressed("move_left"):

rotate_y(0.01) # Rotate on Z axis (positive for left)

elif Input.is_action_pressed("move_right"):

rotate_y(-0.01) # Rotate on Z axis (negative for right)


func _unhandled_input(event):

if event is InputEventKey and event.pressed:

if event.keycode == KEY_W:

translate(Vector3(0, 100, 0) * get_process_delta_time()) # Temporary movement (consider physics)







Ahora sobre esa base como hijo en el Area3D esta el siguiente script:



extends Area3D




var Bullet = preload("res://PROYECTIL.tscn")




func _ready():




pass # Replace with function body.






func _input(event):






if event is InputEventMouseButton:




if event.button_index == 1 and event.pressed :


#event.button_index == MOUSE_BUTTON_LEFT and event.pressed:


var bullet = Bullet.instantiate()







add_child(bullet)

func _unhandled_input(event):

if event is InputEventKey and event.pressed:

if event.keycode == KEY_W:

translate(Vector3(0, 0, 0) * get_process_delta_time()) # Temporary movement (consider physics)


#if event.keycode == KEY_


#apply_impulse(Vector3(0,40,4))#ORIGINAL



if event.keycode == KEY_Z:

rotate_x(0.02)

#pass

if event.keycode == KEY_X:

rotate_x(-0.02)

#pass





---------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------

notas aclaratorias: este script contiene tambien la instaciacion de los proyectiles.



En resumen mirar el video y obserbar la disposicion de los elementos que componen el cañon la camara etc, etc, y la situacion de cada script en cada elemento, y creo que puede serle util a alguien.

martes, 7 de mayo de 2024

GDScript, produce disparos con el mouse, rafagas de disparos girando la rueda del mouse; todo rafagas;para disparos de 1 en 1, solamente presionar 1-----if event.button_index == 1 and event.pressed :;

 extends Area3D


var Bullet = preload("res://PROYECTIL.tscn")


func _ready():


pass # Replace with function body.



func _input(event):



if event is InputEventMouseButton:


#if event.button_index == 1:

#if event.button_index == 1 and event.pressed == false:

var bullet = Bullet.instantiate()



add_child(bullet)


---------------------------------------------------------------------------

para disparos de 1 en 1, sin rafaga, sin que salgan 2 disparos al presionar 1 y soltar 1, solamente presionar 1---------------if event.button_index == 1 and event.pressed :

----------------------------------------------------------------------------

extends Area3D


var Bullet = preload("res://PROYECTIL.tscn")


func _ready():


pass # Replace with function body.



func _input(event):



if event is InputEventMouseButton:


if event.button_index == 1 and event.pressed :

#event.button_index == MOUSE_BUTTON_LEFT and event.pressed:

var bullet = Bullet.instantiate()



add_child(bullet)


------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
al soltar la presion de la tecla del mouse....
--------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
extends Area3D

var Bullet = preload("res://PROYECTIL.tscn")

func _ready():

pass # Replace with function body.


func _input(event):


if event is InputEventMouseButton:

#if event.button_index == 1 and event.pressed :#AL,PRESIONAR
if event.button_index == 1 and not event.pressed :#AL SOLTAR
#event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
var bullet = Bullet.instantiate()


add_child(bullet)

domingo, 5 de mayo de 2024

GDScript; w-a-s-d y flechas teclado , girar y mover un cañon CharacterBody3D;ampliado alante atras izquierda derecha arriba y abajo inclinacion de cañon....!!!!;

 extends CharacterBody3D




var rotate_speed = 10.1 




const SPEED = 5.0

const JUMP_VELOCITY = 14.5


# Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")




func _ready(): 


pass # Replace with function body.


func _physics_process(delta):


if not is_on_floor():

velocity.y -= gravity * delta


# Handle jump.

if Input.is_action_just_pressed("ui_accept") and is_on_floor():

velocity.y = JUMP_VELOCITY


# Get the input direction and handle the movement/deceleration.

# As good practice, you should replace UI actions with custom gameplay actions.

#var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward",)#PERFECTOOOOOOO

var input_dir = Input.get_vector("ui_left", "ui_right","A", "B",)# "A", "B" SOLO AMAGO PARA QUE NO BALLA ADELANTE A DETRAS

#var input_dir = Input.get_vector("A", "D", "W", "S")

var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

if direction:

velocity.x = direction.x * SPEED

velocity.z = direction.z * SPEED

else:

velocity.x = move_toward(velocity.x, 0, SPEED)

velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()



var movimiento_vector = Vector3.ZERO  # Inicializar vector de movimiento


if Input.is_action_pressed("move_forward"):

rotate_x(0.01) # Mover en el eje X para rotar (negativo para abajo)

elif Input.is_action_pressed("move_backward"):

rotate_x(-0.01)   # Mover en el eje X para rotar (positivo para arriba)

elif Input.is_action_pressed("move_left"):

rotate_y(0.01)  # Mover en el eje Z para rotar (positivo para izquierda)

elif Input.is_action_pressed("move_right"):

rotate_y(-0.01) # Mover en el eje Z para rotar (negativo para derecha)


-------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------EXPLICACION: EN GODOT DESDE PROYECTO A CONFIGURACION PROYECTO SE HAN DE ASIGNAR LAS LETRAS move_left" SERIA LA -A-move_right" SERIA LA -D- Y ASI SUCESIVAMENTE, etc etc etc.....



-------------------------------------------------------------------------------------------------------------------------------

ampliado alante atras izquierda derecha arriba y abajo inclinacion de cañon....!!!!!!!!

--------------------------------------------------------------------------------------------------

extends CharacterBody3D




var rotate_speed = 10.1 




const SPEED = 5.0

const JUMP_VELOCITY = 14.5


# Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")




func _ready(): 


pass # Replace with function body.


func _physics_process(delta):


if not is_on_floor():

velocity.y -= gravity * delta


# Handle jump.

if Input.is_action_just_pressed("ui_accept") and is_on_floor():

velocity.y = JUMP_VELOCITY


# Get the input direction and handle the movement/deceleration.

# As good practice, you should replace UI actions with custom gameplay actions.

#var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward",)#PERFECTOOOOOOO

var input_dir = Input.get_vector("ui_left", "ui_right","ui_up", "ui_down",)# "A", "B" SOLO AMAGO PARA QUE NO BALLA ADELANTE A DETRAS

#var input_dir = Input.get_vector("A", "D", "W", "S")

var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

if direction:

velocity.x = direction.x * SPEED

velocity.z = direction.z * SPEED

else:

velocity.x = move_toward(velocity.x, 0, SPEED)

velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()



var movimiento_vector = Vector3.ZERO  # Inicializar vector de movimiento


if Input.is_action_pressed("move_forward"):

rotate_x(0.01) # Mover en el eje X para rotar (negativo para abajo)

elif Input.is_action_pressed("move_backward"):

rotate_x(-0.01)   # Mover en el eje X para rotar (positivo para arriba)

elif Input.is_action_pressed("move_left"):

rotate_y(0.01)  # Mover en el eje Z para rotar (positivo para izquierda)

elif Input.is_action_pressed("move_right"):

rotate_y(-0.01) # Mover en el eje Z para rotar (negativo para derecha)




jueves, 2 de mayo de 2024

script movimiento por teclas w-a-s-d y espacio, GDScript;

extends CharacterBody3D


#func _unhandled_input(event):


const SPEED = 5.0
const JUMP_VELOCITY = 14.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")



func _ready(): 


   # if event is InputEventKey and event.pressed:
   # if event.keycode == KEY_A:


pass # Replace with function body.







func _physics_process(delta):

if not is_on_floor():
velocity.y -= gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward",)
#var input_dir = Input.get_vector("A", "D", "W", "S")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()

-------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------EXPLICACION: EN GODOT DESDE PROYECTO A CONFIGURACION PROYECTO SE HAN DE ASIGNAR LAS LETRAS move_left" SERIA LA -A-move_right" SERIA LA -D- Y ASI SUCESIVAMENTE, SE PODRIA PONER SIMPLEMENTE "a" Y ASIGNAR LA "a" Y ASI SUCESIVAMENTE, PERO ESTA MONTADO ASI.............