Strange behaviour when Rigidbody2D hits trap (Godot 4.1.1)

Hi folks, I’m trying to make it so that when the object (bagel) tries to hit a trap (poop) it will reset the position back to the start (where the marker 2d is named SpawnPosition). However, I’m getting this error as soon as it hits the poop:

Invalid set index ‘velocity’ (on base: ‘RigidBody2d (Bagel)’ with value of type ‘Vector2’ Including scripts in screenshots, along with the node structures for each item affected.

Update: I did in fact read on and turns out that RigidBody uses angular_velocity or linear_velocity, however I tried to change the code on line 20 of the level.gd to linear_velocity and I’m getting a strange effect (where I see a glimpse of the start of the game in a quick flash but still in the same spot). Screenshots included of the code and nodes in the affected scenes (and I did take code from Martian Mike and added in to the Speedy Saucer).





Screenshot 2023-09-17 at 7.10.50 pm
Screenshot 2023-09-17 at 7.10.59 pm
Screenshot 2023-09-17 at 7.11.08 pm

Got a video here: https://imgur.com/a/dABhKHd

Morning,

as a rule, the position of a RigidBody shouldnt be changed directly and only moved by forces as this causes some issues with the physics engine.
which is probably whats happening here, since the physics will be trying to calculate position at the same time.

there are some ways around it by using the Integrate forces, or changing the node type to static and back.
Setting position of a RigidBody2D - Godot Engine - Q&A

but, if its just for a level reset in this game, then reloading the scene will reset the game.
get_tree().reload_current_scene()

Thank you! Would I apply the get_tree code to any object that has the trap.gd script? I ask because I was hoping to branch out to create a start spawnpoint as a scene and an exit one so I move to the next level, or do I just instantiate the player in every level?

Otherwise, would I be able to replicate the same sliding mechanism as a rigidbody with say a characterbody?

Depending on what lecture on just now, in lecture 22 using signals. The reload current scene is used there to restart the game and so the player is returned to the start position when a game over or died scenario is hit. Just the same as loading the game up for the first time .

When it comes to the exit, this can load the next levels scene, and like you say you can have a level 2 scene with the player positioned somewhere else.

Extra levels idea is covered in Martian Mike.

I think there would be a way to get the characterbody behaving similar but would be alot more work.

I had a look around on how to create similar movement of a rigid body as a characterbody and came across this video: https://www.youtube.com/watch?v=KceMokK2qFA

Blockquote
extends CharacterBody2D
#class_name TestBagel
const max_speed = 1000
const accel = 1500
const friction = 600
var input = Vector2.ZERO
func _physics_process(delta):
player_movement(delta)
func get_input():
input.x = int(Input.is_action_pressed(“move_right”)) - int(Input.is_action_pressed(“move_left”))
input.y = int(Input.is_action_pressed(“move_down”)) - int(Input.is_action_pressed(“move_up”))
return input.normalized()
func player_movement(delta):
input = get_input()
if input == Vector2.ZERO:
if velocity.length() > (friction * delta):
velocity -= velocity.normalized() * (friction * delta)
else:
velocity = Vector2.ZERO
else:
velocity += (input * accel * delta)
velocity = velocity.limit_length(max_speed)
move_and_slide()

Works a treat now, and got a solid foundation to work on configuring. :slight_smile: Thank you anyway!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms