lunes, 29 de enero de 2024

instancia muy simple en godot4.2;

 extends Area3D

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


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

func _ready():

pass # Replace with function body.



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

func _process(delta):


var bullet = Bullet.instantiate()

add_child(bullet)

pass



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

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

extends Area3D

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

func _ready():

pass # Replace with function body.

func _process(delta):

var bullet = Bullet.instantiate()

add_child(bullet)

pass


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

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

otra manera que funciona de instanciar en godot4.2

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

extends Area3D


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


var time = 0

var interval = 1.0  # Time interval between bullet instantiation


func _ready():

pass # Replace with function body.


func _process(delta):

time += delta


if time > interval:

time = 0


var bullet = Bullet.instantiate()

add_child(bullet)

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

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

este de debajo me falla pero creo que es por mi configuracion personalizada de teclas en godot algo que e de solucionar

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

extends Area3D


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


var pressed = false


func _ready():

pass # Replace with function body.


func _input(event):

if event is InputEventKey:

if event.scancode == KEY_SPACE:

if not pressed:

pressed = true

var bullet = Bullet.instantiate()

add_child(bullet)

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

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

otro ejemplo, con el boton izquierdo del raton me funciona por que lo e configurado desde un principio asignandole esa bariacion de entradas de teclado desde Proyecto---configuracion de proyecto-----

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

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

extends Area3D


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


func _ready():

pass # Replace with function body.


func _input(event):

if event is InputEventMouseButton:

if event.button_index == 1:

var bullet = Bullet.instantiate()

add_child(bullet)

------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
otro ejemplo al tocar un trigger
------------------------------------------------------------------------------------------------------


extends Area3D


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


func _ready():

    pass # Replace with function body.


func _physics_process(delta):

    # Check if the player is touching the trigger

    if $Trigger.is_colliding():

        var bullet = Bullet.instantiate()

        add_child(bullet)


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

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

este script que funciona solo aplica una fuerza para lanzar un cubo 

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


extends RigidBody3D



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

func _ready():

pass # Replace with function body.



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

func _process(delta):

pass

translate(Vector3(111,0,0) * get_process_delta_time())








extends CharacterBody3D-------para godot4.2 a de tener una camara y solo es para CharacterBody3D-;

 extends CharacterBody3D


const SPEED = 5.0

const JUMP_VELOCITY = 4.5

const FRICTION = 25

const HORIZONTAL_ACCELERATION = 30

const MAX_SPEED=5

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

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


@onready var camera = $Camera3D




func _ready():

Input.mouse_mode=Input.MOUSE_MODE_CAPTURED


func _unhandled_input(event):

if event is InputEventMouseMotion and Input.mouse_mode==Input.MOUSE_MODE_CAPTURED:

rotate_y(-event.relative.x * .005)

camera.rotate_x(-event.relative.y * .005)

camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)


func _unhandled_key_input(event):

if Input.is_action_just_pressed("ui_cancel"):

if Input.mouse_mode==Input.MOUSE_MODE_CAPTURED: 

Input.mouse_mode=Input.MOUSE_MODE_VISIBLE

else:

Input.mouse_mode=Input.MOUSE_MODE_CAPTURED


func _physics_process(delta):

# Add the gravity.

if not is_on_floor():

velocity.y -= gravity * delta


# Handle Jump.

if Input.is_action_just_pressed("ui_accept") and is_on_floor() and Input.mouse_mode==Input.MOUSE_MODE_CAPTURED:

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 = Vector3.ZERO

var movetoward = Vector3.ZERO

input_dir.x = Input.get_vector("move_left", "move_right", "move_forward", "move_backward").x

input_dir.y = Input.get_vector("move_left", "move_right", "move_forward", "move_backward").y

input_dir=input_dir.normalized()

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

direction *= SPEED

velocity.x = move_toward(velocity.x,direction.x, HORIZONTAL_ACCELERATION * delta)

velocity.z = move_toward(velocity.z,direction.z, HORIZONTAL_ACCELERATION * delta)


var angle=5

#rotation_degrees=Vector3(input_dir.normalized().y*angle,rotation_degrees.y,-input_dir.normalized().x*angle)

var t = delta * 6

if Input.mouse_mode==Input.MOUSE_MODE_CAPTURED: 

rotation_degrees=rotation_degrees.lerp(Vector3(input_dir.normalized().y*angle,rotation_degrees.y,-input_dir.normalized().x*angle),t)

move_and_slide()

force_update_transform()


Script para Godot version3.5 mover un objeto con teclas;

 extends RigidBody


var velocity = Vector3()


func _physics_process(delta):

# Obtener la velocidad actual del RigidBody

velocity = get_linear_velocity()


# Ajustar la velocidad según las teclas presionadas

if Input.is_action_pressed("ui_left"):

velocity.x -= 1

if Input.is_action_pressed("ui_right"):

velocity.x += 1

if Input.is_action_pressed("ui_up"):

velocity.y += 1

if Input.is_action_pressed("ui_down"):

velocity.y -= 1


# Aplicar la nueva velocidad al RigidBody

set_linear_velocity(velocity)

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

otra forma

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

extends RigidBody


var velocity = Vector2()


func _physics_process(delta):

# Obtener la velocidad actual del RigidBody

velocity = get_linear_velocity()


# Ajustar la velocidad según las teclas presionadas

if Input.is_action_pressed("ui_left"):

velocity.x -= 10 * delta

if Input.is_action_pressed("ui_right"):

velocity.x += 10 * delta

if Input.is_action_pressed("ui_up"):

velocity.y += 10 * delta

if Input.is_action_pressed("ui_down"):

velocity.y -= 10 * delta


# Aplicar la nueva velocidad al RigidBody

set_linear_velocity(velocity)

----------------------------------------------------------------------------------------------------------------------
otra combinacion

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

extends RigidBody




var velocity = Vector2()



func _physics_process(delta):

# Obtener la velocidad actual del RigidBody

velocity = get_linear_velocity()



# Ajustar la velocidad según las teclas presionadas

if Input.is_action_pressed("ui_left"):

velocity.x -= 10 * delta

if Input.is_action_pressed("ui_right"):

velocity.x += 10 * delta

if Input.is_action_pressed("ui_up"):

velocity.z -= 10 * delta

if Input.is_action_pressed("ui_down"):

velocity.z += 10 * delta



# Aplicar la nueva velocidad al RigidBody

set_linear_velocity(velocity)




------------------------------------------------------------------------------------------------------------
otra version alante atras izquierda derecha suve y baja
------------------------------------------------------------------------------------------------------
extends RigidBody




var velocity = Vector2()



func _physics_process(delta):

# Obtener la velocidad actual del RigidBody

velocity = get_linear_velocity()



# Ajustar la velocidad según las teclas presionadas

if Input.is_action_pressed("ui_left"):

velocity.x -= 10 * delta

if Input.is_action_pressed("ui_right"):

velocity.x += 10 * delta

if Input.is_action_pressed("ui_up"):

velocity.z -= 10 * delta

if Input.is_action_pressed("ui_down"):

velocity.z += 10 * delta



if Input.is_action_pressed("ui_accept"):
velocity.y += 10 * delta
if Input.is_action_just_pressed("ui_end"):
velocity.y -= 20 * delta






# Aplicar la nueva velocidad al RigidBody

set_linear_velocity(velocity)


-------------------------------------------------------------
otra girando sobre si mismo una parte
-------------------------------------------------------------------------------

extends RigidBody




var velocity = Vector2()



func _physics_process(delta):

# Obtener la velocidad actual del RigidBody

velocity = get_linear_velocity()



# Ajustar la velocidad según las teclas presionadas

if Input.is_action_pressed("ui_left"):
   rotate_y(-10 * delta)
#velocity.x -= 10 * delta

if Input.is_action_pressed("ui_right"):

velocity.x += 10 * delta

if Input.is_action_pressed("ui_up"):

velocity.z -= 10 * delta

if Input.is_action_pressed("ui_down"):

velocity.z += 10 * delta



if Input.is_action_pressed("ui_accept"):
velocity.y += 10 * delta
if Input.is_action_just_pressed("ui_end"):
velocity.y -= 20 * delta






# Aplicar la nueva velocidad al RigidBody

set_linear_velocity(velocity)