So im developing this 3D based topdown game in Godot 3.3.4. I've wrote some code for the player movement but it causes the player to slowly slide while standing on ramps.
I kinda can imagine how to fix it but I think im over engineering a prety simple fix, so im asking the comunity. Is there a simple way to make the player stay on place while touching groud?
(Im aware of move_and_slide_with_snap() method but i couldn't make it work correctly either) (im also aware i forgot aplying delta)
Here's the code:
export var speed = 7.5 const GRAVITY = Vector3.DOWN * 100 var velocity = Vector3.ZERO func _physics_process(delta): velocity += GRAVITY * delta velocity = move_and_slide(velocity, Vector3.UP) var input_vector = Vector2.ZERO input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left") input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up") input_vector = input_vector.normalized() velocity.x = input_vector.x * speed velocity.z = input_vector.y * speed move_and_slide(velocity)
This is my first community ask so hi and thank u.
I got some gifs but apparently u need 10 rep and im new so...
Thank you Theraot, i've enabled the slope thing but than just worked sometimes. So I've made some changes and i think im okay with this:
extends KinematicBody
export var speed = 750
const GRAVITY = Vector3.DOWN * 10
var velocity = Vector3.ZERO
func _physics_process(delta):
velocity += GRAVITY * delta
velocity = move_and_slide_with_snap(velocity, Vector3.DOWN, Vector3.UP, true, 1, 0.80)
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
velocity.x = input_vector.x * speed * delta
velocity.z = input_vector.y * speed * delta
move_and_slide_with_snap(velocity, Vector3.DOWN, Vector3.UP, true, 0, 0.785398)
Im shure that code is pretty much to work on but the movement is okey and it only slides a bit on big ramps so that can be a "fun" thing.
Im closing this but if somebody want to correct that code im okay with it. Have fun.
Have a look at the parameters of move_and_slide
:
Vector3 move_and_slide ( Vector3 linear_velocity, Vector3 up_direction=Vector3( 0, 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )
It says bool stop_on_slope=false
. I believe that is the one you want.
So your move_and_slide
call would look like this:
velocity = move_and_slide(velocity, Vector3.UP, true)
I also want to mention that the up_direction
and floor_max_angle
are used to decide what is a floor/slope.
Addendum
it only slides a bit on big ramps
If the issue is steep ramps, try increasing that floor_max_angle
. The value of 0.785398
is an eight of a turn (TAU * 0.125
, radians, of course). Or 45 degrees, if you prefer. 0.8
is not much more, it comes as 45.8 degrees (TAU * 0.127324
).
Something else: you are doing two calls, it seems to be that the vertical velocity would be twice what it would be for the gravity, at least while in the air. Thinking about that gave me another idea. If you use move_and_collide
for the vertical component (i.e. gravity only), there would be no sliding. And then you can use move_and_slide
or move_and_slide_with_snap
for the other movement.
By the way, the "snap" in move_and_slide_with_snap
refers to something related but not the same.
Consider a character on ramp, moving with the horizontal direction the ramp goes down, but not going down fast enough (because the ramp is too steep and the horizontal speed is too large for it, or gravity is too low). And without snapping, the character goes in the air. So, yes, snapping is important, but NOT necessarily the solution for sliding.
Snapping is also useful for having the character stick to walls. For example for wall running.
And what decides what is floor and what is wall? The up_direction
and floor_max_angle
. It could be that your character "slides a bit" because the ramp is not counting as floor, but as wall.