#Source Physics

10 messages · Page 1 of 1 (latest)

split cairn
#

hey, does anyone have any tips on making the players momentum carry through when they're falling on a slope? I'm trying to replicate source movement all is well but when I fall on a slope once the player leaves the slope they fall straight down instead if falling on an angle. I'm bad at explaining it, ill attach a video of what i mean. i've already asked ai a few different ways but it doesn't understand what im asking it, im definitely explaining it wrong but i can't think of the right words to use

#
    
# Movement
const MAX_VELOCITY_AIR = 0.6
const MAX_VELOCITY_GROUND = 6.0
const MAX_ACCELERATION = 10 * MAX_VELOCITY_GROUND
const GRAVITY = 20#15.34
const STOP_SPEED = 1.5
const JUMP_IMPULSE = sqrt(3 * GRAVITY * 0.85)
    
var direction = Vector3()
var wish_jump
var effective_gravity
    
var friction = 4

var sensitivity = 0.15```
#
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _physics_process(delta):
    process_input()
    process_movement(delta)```
#
    direction = Vector3()
    
    # Movement directions
    if Input.is_action_pressed("forward"):
        direction -= transform.basis.z
    elif Input.is_action_pressed("back"):
        direction += transform.basis.z
    if Input.is_action_pressed("left"):
        direction -= transform.basis.x
    elif Input.is_action_pressed("right"):
        direction += transform.basis.x
        
    # Jumping
    wish_jump = Input.is_action_just_pressed("Jump")

func process_movement(delta):
    # Get the normalized input direction so that we don't move faster on diagonals
    var wish_dir = direction.normalized()```
#
        if wish_jump:
            velocity.y = JUMP_IMPULSE
            # Update velocity as if we are in the air
            velocity = update_velocity_air(wish_dir, delta)
            wish_jump = false
        else:
            velocity = update_velocity_ground(wish_dir, delta)
        
    else:
        # Only apply gravity while in the air
        velocity.y -= GRAVITY * delta
        velocity = update_velocity_air(wish_dir, delta)

    # Move the player once velocity has been calculated
    move_and_slide()
    
func _input(event):
    # Camera rotation
    if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
        _handle_camera_rotation(event)```
#
    # Rotate the camera based on the mouse movement
    rotate_y(deg_to_rad(-event.relative.x * sensitivity))
    $Head.rotate_x(deg_to_rad(-event.relative.y * sensitivity))
    
    # Stop the head from rotating to far up or down
    $Head.rotation.x = clamp($Head.rotation.x, deg_to_rad(-90), deg_to_rad(90))
    
func accelerate(wish_dir: Vector3, max_velocity: float, delta):
    # Get our current speed as a projection of velocity onto the wish_dir
    var current_speed = velocity.dot(wish_dir)
    # How much we accelerate is the difference between the max speed and the current speed
    # clamped to be between 0 and MAX_ACCELERATION which is intended to stop you from going too fast
    var add_speed = clamp(max_velocity - current_speed, 0, MAX_ACCELERATION * delta)
    
    return velocity + add_speed * wish_dir
    
func update_velocity_ground(wish_dir: Vector3, delta):
    # Apply friction when on the ground and then accelerate
    var speed = velocity.length()
    
    if speed != 0:
        var control = max(STOP_SPEED, speed)
        var drop = control * friction * delta
        
        # Scale the velocity based on friction
        velocity *= max(speed - drop, 0) / speed
    
    return accelerate(wish_dir, MAX_VELOCITY_GROUND, delta)
    
func update_velocity_air(wish_dir: Vector3, delta):
    var new_velocity = accelerate(wish_dir, MAX_VELOCITY_AIR, delta)

    # Calculate the current slope angle
    return accelerate(wish_dir, MAX_VELOCITY_AIR, delta)```
#

thats the player movement script

uneven cosmos
#

I only had a quick look over your code, but you might be interested in some of the basic character3D properties, maybe try to set floor_stop_on_slope to false and changing floor_max_angle

https://docs.godotengine.org/en/stable/classes/class_characterbody3d.html

#

maybe you could also make use of get_floor_angle() and add it to the velocity?

split cairn
#

I had a play around with floor_stop_on_slope to false and changing the max angle. I think it might not be detecting the slope below the player