extends KinematicBody
var speed=10
var mouse_sensitivity=0.5
var direction=Vector3.ZERO
onready var head=$Head
func _ready():
if Input.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x*mouse_sensitivity))
head.rotate_x(deg2rad(-event.relative.y*mouse_sensitivity))
head.rotation.x=clamp(head.rotation.x,deg2rad(-90),deg2rad(90))
func _process(delta):
Vector3.ZERO
if Input.is_action_pressed("mf"):
direction-=transform.basis.z
elif Input.is_action_pressed("b"):
direction+=transform.basis.z
direction=direction.normalized()
move_and_slide(direction*speed,Vector3.UP)
I don't know what i'm doing wrong.
My kinematicBody keeps moving after released a key.
Pay attention to direction:
var direction=Vector3.ZERO
# ...
func _process(delta):
Vector3.ZERO
if Input.is_action_pressed("mf"):
direction-=transform.basis.z
elif Input.is_action_pressed("b"):
direction+=transform.basis.z
direction=direction.normalized()
move_and_slide(direction*speed,Vector3.UP)
It starts as ZERO. Then you press one of the recognized inputs, and you modify it. It is no longer ZERO after than input.
Of course, subsequent executions of _process would not enter the branches where you modify direction. However, direction retained its value.
This line will run regardless of input:
move_and_slide(direction*speed,Vector3.UP)
It does not move when direction is ZERO. But again, after the first input direction is not ZERO anymore.
I believe you meant to write direction = Vector3.ZERO as the first line of _process.
I remind you to pay attention to your warnings:
You are not using delta. May mean the code does not compensate for variations in frame rate.
You a standalone expression (Vector3.ZERO). That does nothing. Did you meant to do something there?
You are discarding the return value of move_and_slide. To be honest, I usually tell Godot to ignore this one.
It is a good sign to have your script without warnings. Review them. And if you decide that there is nothing wrong, you can tell Godot to ignore them (this can be done done with a comment # warning-ignore:.... See GDScript warning system). Telling Godot to ignore warnings will make it easier to notice when a new warning pops up.
Something else, I believe this code should be in _physics_process. This is from move_and_slide documentation:
This method should be used in Node._physics_process (or in a method called by Node._physics_process), as it uses the physics step's delta value automatically in calculations. Otherwise, the simulation will run at an incorrect speed.
Related
I want the inscription "hello" to be displayed in case of a collision with an object! How to do it? And what should be used?
I tried to do this using the Area2D node, but nothing worked :(
I tried to use the following code:
func print_msg():
print("Hello")
func _on_area_entered(): (I attached it)
print_msg()
Before that, I made a player using KinematicBody2D and made him move. And made blocks for walking
Since you've left things very ambiguous I'll try to explain it to the best of what I understood
Here I have the basic setup with a RigidBody2D and Area2D:
And here's the main script:
extends Node2D
func message_area(thing):
print("Hello! (Area Entered)")
func message_body(thing): # you were probably missing this
print("Hello! (Body Entered)")
func _ready():
$Area2D.connect("area_entered",self,"message_area")
$Area2D.connect("body_entered",self,"message_body")
Now when you press play the rigid body will fall into Area2D and when it does it will output this:
Hello! (Body Entered)
My best guess is you only connected the area_entered signal but not the body_entered which is why nothing was being printed when the object entered the Area2D
You can further follow on how to connect signals & How Area2D works
And I'd recommend you post code or explain what you've tried (preferably with images) so people can understand your problem better and help better :)
Edit:
If you want to change the visibility of the thing entered/collided you have 2 options:
...
func message_body(thing):
print("Hello! (Body Entered)")
thing.visible=true #option 1
thing.modulate.a=0.0 #option 2 (you can also use self_modulate)
Option 1 is the equivalent of pressing the eye icon
Option 2 changes the alpha value i.e. decides how much transparent your object should be and lies between 0 & 1 where 0=completely invisible, 1=completely visible, 0.5=50% visible and so on...
Equivalent of changing :
However, since you said "with a certain object" you can add a condition before toggling the visibility:
func message_body(thing):
print("Hello! (Body Entered)")
if(thing is KinematicBody2D): # or you can create a custom class and use it's `class_name`
thing.visible=true
thing.modulate.a=0.0
# or
if(thing.name =="Main Player"):
thing.visible=true
thing.modulate.a=0.0
# etc etc
I recommend you read this if you already haven't
I am having problem figure out how the normal work.
I am using godot4 RC1.
I created a staticbody3D and want to place a 3dobject standing upright like how we stand on earth.
this is my code for the staticbody3d:
func _on_input_event(camera, event, position, normal, shape_idx):
player.global_position = position
var q = Quaternion.from_euler(normal)
player.basis = q
basically I use the code to capture the mouse_over position on the staticbody3d and place my player(Mesh3d) at the position, and align the player basis to the normal.
if mouse at the top of the planet, it turn out ok.
but anywhere else, it just gone hay wire:
How can I resolve this?
I am currently trying to make a small top-down RPG with grid movement.
To simplify things, when i need to make something move one way, I use a RayCast2D Node and see if it collides, to know if i can move said thing.
However, it does not seem to detect the walls ive placed until I am inside of them.
Ive already checked for collision layers, etc and they seem to be set up correctly.
What have i done wrong ?
More info below :
Heres the code for the raycast check :
func is_path_obstructed_by_obstacle(x,y):
$Environment_Raycast.set_cast_to(Vector2(GameVariables.case_width * x * rayrange, GameVariables.case_height * y * rayrange))
return $Environment_Raycast.is_colliding()
My walls are from a TileMap, with collisions set up. Everything that uses collisions is on the default layer for now
Also heres the function that makes my character move :
func move():
var direction_vec = Vector2(0,0)
if Input.is_action_just_pressed("ui_right"):
direction_vec = Vector2(1,0)
if Input.is_action_just_pressed("ui_left"):
direction_vec = Vector2(-1,0)
if Input.is_action_just_pressed("ui_up"):
direction_vec = Vector2(0,-1)
if Input.is_action_just_pressed("ui_down"):
direction_vec = Vector2(0,1)
if not is_path_obstructed(direction_vec.x, direction_vec.y):
position += Vector2(GameVariables.case_width * direction_vec.x, GameVariables.case_height * direction_vec.y)
grid_position += direction_vec
return
With ray casts always make sure it is enabled.
Just in case, I'll also mention that the ray cast cast_to is in the ray cast local coordinates.
Of course collision layers apply, and the ray cast has exclude_parent enabled by default, but I doubt that is the problem.
Finally, remember that the ray cast updates on the physics frame, so if you are using it from _process it might be giving you outdated results. You can call call force_update_transform and force_raycast_update on it to solve it. Which is also what you would do if you need to move it and check multiple times on the same frame.
For debugging, you can turn on collision shapes in the debug menu, which will allow you to see them when running the game, and see if the ray cast is positioned correctly. If the ray cast is not enabled it will not appear. Also, by default, they will turn red when they collide something.
So, the problem i'm struggling with is making collectibles disappear properly and making it so the playable character doesn't collide with them at all. My guess (i'm still very new to the engine) is that either the stomp mechanic of the playable character is somehow colliding with the collectibles' hitbox or i messed up the collectibles' hitbox settings.
My guess for the animation is that i missed a step while coding the animation switch, i still now pretty much nothing of the AnimatedSprite node since i've used the AnimationPlayer so far (someone has suggested me to use the AnimatedSprite node to animate 2D sprites instead of the AnimationPlayer so i'm trying it out)
Here follows a portion of the playable character's code.
#Stomp mechanic
func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2: #Schiacciare i nemici
var output = linear_velocity
output.y = -impulse
return output
func _on_EnemyDetector_area_entered(area):
_velocity = calculate_stomp_velocity(_velocity, stomp_impulse)
#Death
func _on_EnemyDetector_body_entered(body):
_velocity.y = 0.0
queue_free()
Here is instead the collectibles' code (the collectible's name is "Leaf")
extends Area2D
onready var _animated_sprite = $AnimatedSprite
func _process(delta):
_animated_sprite.play("Floating")
func _on_Leaf_body_entered(KinematicBody2D) -> void:
_animated_sprite.stop()
_animated_sprite.play("Fade")
queue_free()
The function queue_free will delete the node between frames. Thus, in this code:
_animated_sprite.play("Fade")
queue_free()
The animation does not get a chance to be seen.
I suggest you yield to the "animation_finished" signal:
_animated_sprite.play("Fade")
yield(_animated_sprite, "animation_finished")
queue_free()
When Godot is executing the code and encounters yield, it will stop executing the function. And when the signal ("animation_finished") is emitted, the Godot will resume the exeution. That way the animation gets to play before the node is removed.
You may also want to disable the Area2D. For example: set_deferred("monitoring", false). So it does no longer trigger "body_entered". I will also mention that it is a common practice to use node groups to identify nodes on collision.
I am trying to teleport my player node(kinematicbody2d) when it hits my finish node (area2d) from the side of the Finish node
BTW I'm using godot 3
What I tried:Change the location using get_node("player").set_pos and get_node("player").location
code:
extends Area2D
func _on_Finish12_body_entered(body):
if body.get_name() == "player":
print("%s touched the finish on level %s" % [body.get_name(), get_tree().get_current_scene().get_name()])
get_node("player").position = Vector2(1504, 1896)
pass
So what I need:
The playing being teleported to 1504, 1896
There's a lot of unknowns here that can be the problem
Is the player's location updated in other parts of the code? and if so, could it be possible you did move to 1504, 1896 but then immediately get clobbered by said code?
What is the current behavior when you apply the new position? Does your player move at all? Does it go somewhere unintended?
Does your print statement execute?
Have you tried using move_and_slide/move_and_collide for the kinematicBody to check for collision?
Just a few ideas on how to go about figuring it out.
This is what works with Area and KinematicBody (i.e. 3D):
extends Area
func _on_Area_body_entered(body):
body.look_at_from_position(spawn, Vector3(0,0,0), Vector3(0,0,0))
with spawn being an empty spatial to define the point in space to teleport to.