0 votes

I didn't like the floatiness of velocity controls, so I made a control system with setpos(). And it controls much better. However I have ran into a issue. the setpos() function overrides collision. So I can move my character smoothly, smoothly through walls. How do make the set_pos() not override the collision of my scene.

#Get position
var charx= get_pos().x setget setcharx
var chary= get_pos().y setget setchary

func _ready():
    set_fixed_process(true)
    pass

func _fixed_process(delta):
    #control variables
    var moveu = Input.is_action_pressed("ui_up")
    var moved = Input.is_action_pressed("ui_down")
    var mover = Input.is_action_pressed("ui_right")
    var movel = Input.is_action_pressed("ui_left")
    var controlenabled = true

    #input
    if controlenabled == true:
        if (moveu):
            setchary(-5)
            pass
        if (moved):
            setchary(5)
            pass
        if (mover):
            setcharx(5)
            pass
        if (movel):
            setcharx(-5)
            pass
        pass

    set_pos(Vector2(charx,chary))
    pass

func setcharx(value):
    charx = value + get_pos().x
    pass

func setchary(value):
    chary = value + get_pos().y
    pass
in Engine by (272 points)

1 Answer

+2 votes
Best answer

You can't.

It's one or the other. If you're using KinematicBody you should be able to get the same effect as set_pos() with move_to().

by (5,248 points)
selected by

Thank you! However I now have a different problem, It sticks to objects if it's rubbing up against them. Any way to fix that?

Hard to say. Kinematics aren't used for their collision responsiveness. Often that's why they're preferred, so they will ignore certain impacts. IE - Moving platforms.

If you're writing your own physics code, you have to figure out what all these situations are, study the outputs, and fine tune solutions to your design. There are all kinds of techniques, such as casting rays, using special collision shapes, etc. There is a lot of study, and various pros and cons, so I'm afraid there is no easy answer that will be the magic bullet there.

I'm fairly sure the standard thing is to cast a ray, and detect if it's a wall collision. Then ignore whatever movement is being sent, and maybe even perform a vector slide along the collision normal. Such as this: http://docs.godotengine.org/en/stable/tutorials/2d/kinematic_character_2d.html#problem

I could also suggest some kinematic character tutorial videos @ https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz

It's for Unity, but it may give you some ideas about the techniques and considerations you need to tackle when writing kinematic characters.

I hope that helps.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.