0 votes

I want a kinematic body to be limited within the bounds of a circle, which I've done with this function:

func circle_limits(radius: float, center: Vector2, location: Vector2, pull_force: float):
    var distance = center.distance_to(location)

    if distance > radius:
        var origin_to_object = (location - center) * (radius / distance)
        return lerp(location, center + origin_to_object, pull_force)
    return location

This works how I want it to, but since I'm using the value to directly manipulate the position of my player, it doesn't apply any collisions when limiting the movement, causing the object to go through walls. Is there any way to use this value to limit the player within the confines of the circle while still applying collisions?

Godot version 3.4
in Engine by (19 points)

1 Answer

0 votes

I asked a similar question a few days ago in the Godot Forum
The gist of the answer is to not use a lerp but the moveandslide (or collide) function. The slide version will do the collision itself while with moveandcollide you have to do it yourself. Maybe its enough to just interchange the lerp with an appropriate call to moveandslide and don't call the function if the distance is too big as you already do with the lerp right now.

by (102 points)
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 webmaster@godotengine.org with your username.