Godot Version
4.2.1.stable
Question
Hello! I am currently working on a 3D puzzle platformer (it will be a sidescroller, but I am looking to keep the art style of a 3D game, as everything in the background will be cardboard cutouts and other “arts and crafts”-esque objects.
While I have figured out movement and applying a force to the rigid body my character is colliding with, I am having trouble with the character rabidly accelerating towards infinity, along with the rigid body it is colliding with.
Additionally, the object will simply not move occasionally, and it is seemingly randmo when I can push the object and when I cannot.
I would like to avoid using impulses, as was my original plan, because it is jittery and I am planning on slowly pushing these objects throughout the map instead of simply applying one-time forces.
CODE:
extends CharacterBody3D
class_name CharacterBody
@export var SPEED = 2
@export var JUMP_VELOCITY =4
@export var PUSH_FORCE = 25
@export_range(0.0, 1.0) var FRICTION = 0.1
@export_range(0.0, 1.0) var ACCELERATION = 0.25
#get gravity from project settings
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta):
#falling
velocity.y -= gravity * delta
#movement
var dir = Input.get_axis("input_left", "input_right")
if dir != 0:
velocity.x = lerp(velocity.x, dir * SPEED, ACCELERATION)
else:
velocity.x = lerp(velocity.x, 0.0, FRICTION)
#jumping
if Input.is_action_just_pressed("input_jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
move_and_slide()
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody3D:
c.get_collider().apply_central_force(-c.get_normal() * PUSH_FORCE)